Keayboard action

Just starting out? Need help? Post your questions and find answers here.
PowerSoft
User
User
Posts: 65
Joined: Sun Aug 16, 2015 2:54 pm

Keayboard action

Post by PowerSoft »

I wont to change the variable FacingAzimuth with the left- or right key.
The code is working but when I tough the key for a short moment PB is so fast the value is change to much.
How can I change the the code in the way that the value is increment or decrement with one independent of how long I
push the button.

Any help will be great

Code: Select all

      ExamineKeyboard()
      If KeyboardReleased(#PB_Key_Escape)
        Exit = #True
      EndIf  
      
      If KeyboardPushed(#PB_Key_Left)
        ;Debug "#PB_Key_Left"
        FacingAzimuth=FacingAzimuth-1
        If FacingAzimuth < 0
          FacingAzimuth = 0
        EndIf
      EndIf
      
      If KeyboardPushed(#PB_Key_Right)
        ;Debug "#PB_Key_Right"
        FacingAzimuth=FacingAzimuth+1
        If FacingAzimuth > 360
          FacingAzimuth = 360
        EndIf
      EndIf
OS X 10.10.5 PB 5.31(x64)
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Keayboard action

Post by netmaestro »

Try a Delay(<some value>) after If KeyboardPushed... and before FacingAzimuth + 1

A delay of 30 or so might do, but try values until you hit one you like.
BERESHEIT
User avatar
kenmo
Addict
Addict
Posts: 2046
Joined: Tue Dec 23, 2003 3:54 am

Re: Keayboard action

Post by kenmo »

Use something like:

Code: Select all

LeftPrev = LeftNow
LeftNow = KeyboardPushed(#PB_Key_Left)
If (LeftNow and (Not LeftPrev))
  ;Debug "#PB_Key_Left"
  FacingAzimuth=FacingAzimuth-1
  If FacingAzimuth < 0
    FacingAzimuth = 0
  EndIf
EndIf
User avatar
kenmo
Addict
Addict
Posts: 2046
Joined: Tue Dec 23, 2003 3:54 am

Re: Keayboard action

Post by kenmo »

If you want the angle to continue changing while key is held, try something like:

Code: Select all

If KeyboardPushed(#PB_Key_Left)
  If (LeftCount = 0)
    ;Debug "#PB_Key_Left"
    FacingAzimuth=FacingAzimuth-1
    If FacingAzimuth < 0
      FacingAzimuth = 0
    EndIf
  EndIf
  LeftCount = (LeftCount + 1) % 10
Else
  LeftCount = 0
EndIf
Or using floats:

Code: Select all

If KeyboardPushed(#PB_Key_Left)
  ;Debug "#PB_Key_Left"
  FacingAzimuth.f=FacingAzimuth-0.1
  If FacingAzimuth < 0
    FacingAzimuth = 0
  EndIf
EndIf
Post Reply