November 25, 2020, 08:14:27 AM

Author Topic: [bb] 4 key states (Up/Idle, ToDown/Pressed, Down/Held, ToUp/Released) by RemiD [ 6 months ago ]  (Read 540 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : 4 key states (Up/Idle, ToDown/Pressed, Down/Held, ToUp/Released)
Author : RemiD
Posted : 6 months ago

Description : This demonstrates how to get 4 different states for each key instead of the default 3 (up, hit, down)

Code :
Code: BlitzBasic
  1. ;4 key states (Up/Idle, ToDown/Pressed, Down/Held, ToUp/Released)
  2. ;This demonstrates how to get 4 different states for each key instead of the default 3 (up, hit, down)
  3. ;i have coded my own keys/input system using this, but this is just a simple example so that you can understand the concept and add it to your own keys/input system...
  4. Graphics3D(640,480,32,2)
  5.  
  6. SeedRnd(MilliSecs())
  7.  
  8. ;for each key you need to have these globals in order to store the state of the key, or you can create arrays and have a constant for each key which corresponds to its index in the array...
  9. Global KeySpaceState%
  10. Global KeySpacePrevState%
  11. Global KeySpaceDownCount%
  12.  
  13. Const CUp% = 1 ;Idle
  14. Const CToDown% = 2 ;Pressed
  15. Const CDown% = 3 ;Held
  16. Const CToUp% = 4 ;Released
  17.  
  18. Global Timer = CreateTimer(10)
  19.  
  20. While(KeyDown(1)<>1)
  21.  
  22.  GetInput()
  23.  
  24.  ;SetBuffer(BackBuffer())
  25.  ;ClsColor(000,000,000)
  26.  ;Cls()
  27.  ;Locate(0,0)
  28.  
  29.  If( KeySpaceState = CUp )
  30.   Print("Up/Idle")
  31.  ElseIf( KeySpaceState = CToDown )
  32.   Print("ToDown/Pressed")
  33.  ElseIf( KeySpaceState = CDown )
  34.   Print("Down/Held")
  35.  ElseIf( KeySpaceState = CToUp )
  36.   Print("ToUp/Released")
  37.  EndIf
  38.  
  39.  WaitTimer(Timer)
  40.  VWait():Flip()
  41.  
  42. Wend
  43.  
  44. End()
  45.  
  46. Function GetInput()
  47.  KeySpaceState = CUp
  48.  If( KeyDown(57)=1 )
  49.   KeySpaceDownCount = KeySpaceDownCount + 1
  50.   If( KeySpaceDownCount = 1 )
  51.    KeySpaceState = CToDown
  52.    KeySpacePrevState = CToDown
  53.   ElseIf( KeySpaceDownCount > 1 )
  54.    KeySpaceState = CDown
  55.    KeySpacePrevState = CDown
  56.   EndIf
  57.  ElseIf( KeyDown(57)=0 )
  58.   KeySpaceDownCount = 0
  59.   If( KeySpacePrevState = CToDown Or KeySpacePrevState = CDown )
  60.    KeySpaceState = CToUp
  61.    KeySpacePrevState = CToUp
  62.   EndIf
  63.  EndIf
  64.  ;DebugLog("KeySpaceState = "+KeySpaceState)
  65. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal