January 28, 2020, 02:52:01 PM

Author Topic: [bb] Raw keyboard functions by sswift [ 1+ years ago ]  (Read 861 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Raw keyboard functions by sswift [ 1+ years ago ]
« on: June 29, 2017, 12:28:42 AM »
Title : Raw keyboard functions
Author : sswift
Posted : 1+ years ago

Description : Want to check to see if the user has pressed enter in a textfield?

Want to see if the user is holding shift when they clicked the mouse over your canvas, even though that naughty textfield still has focus?

WELL YOU CAN'T!

But now you can!

This function will allow you to do this AND you have the option of getting this info even if your application doesn't currently have focus.


Code :
Code: BlitzBasic
  1. ; -------------------------------------------------------------------------------------------------------------------------------------
  2. ; To use these functions, place the following lines in user32.decls in your userlib directory:
  3. ;
  4. ;       .lib "user32.dll"
  5. ;       GetAsyncKeyState%(vKey%)
  6. ;       GetActiveWindow%()
  7. ;
  8. ; More info:
  9. ; http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/WindowsUserInterface/UserInput/VirtualKeyCodes.asp
  10. ; http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/keyboardinput/keyboardinputreference/keyboardinputfunctions/getasynckeystate.asp
  11. ; -------------------------------------------------------------------------------------------------------------------------------------
  12.  
  13. ; Common keys:
  14. Const VK_LEFTMOUSE                              = $01   ; Left physical mouse button
  15. Const VK_RIGHTMOUSE                     = $02   ; Right physical mouse button
  16. Const VK_MIDDLEMOUSE                    = $04   ; Middle physical mouse button
  17. Const VK_BACKSPACE                              = $08   ; BACKSPACE key
  18. Const VK_TAB                                    = $09   ; TAB key
  19. Const VK_ENTER                                  = $0D   ; ENTER key (Main keyboard or keypad)
  20. Const VK_SHIFT                                  = $10   ; SHIFT key (Left or right)
  21. Const VK_CONTROL                                = $11   ; CTRL key (Left or right)
  22. Const VK_ALT                                    = $12   ; ALT key (Left or right)
  23. Const VK_PAUSE                                  = $13   ; PAUSE key
  24. Const VK_CAPITAL                                = $14   ; CAPS LOCK key
  25. Const VK_ESCAPE                                 = $1B   ; ESC key
  26. Const VK_SPACE                                  = $20   ; SPACEBAR
  27. Const VK_PGUP                                   = $21   ; PAGE UP key
  28. Const VK_PGDN                                   = $22   ; PAGE DOWN key
  29. Const VK_END                                    = $23   ; END key
  30. Const VK_HOME                                   = $24   ; HOME key
  31. Const VK_LEFT                                   = $25   ; LEFT ARROW key
  32. Const VK_UP                                     = $26   ; UP ARROW key
  33. Const VK_RIGHT                                  = $27   ; RIGHT ARROW key
  34. Const VK_DOWN                                   = $28   ; DOWN ARROW key
  35. Const VK_PRINTSCRN                              = $2C   ; PRINT SCREEN key
  36. Const VK_INSERT                                 = $2D   ; INSERT key
  37. Const VK_DELETE                                 = $2E   ; DELETE key
  38. Const VK_0                                              = $30
  39. Const VK_1                                              = $31
  40. Const VK_2                                              = $32
  41. Const VK_3                                              = $33
  42. Const VK_4                                              = $34
  43. Const VK_5                                              = $35
  44. Const VK_6                                              = $36
  45. Const VK_7                                              = $37
  46. Const VK_8                                              = $38
  47. Const VK_9                                              = $39
  48. Const VK_A                                              = $41
  49. Const VK_B                                              = $42
  50. Const VK_C                                              = $43
  51. Const VK_D                                              = $44
  52. Const VK_E                                              = $45
  53. Const VK_F                                              = $46
  54. Const VK_G                                              = $47
  55. Const VK_H                                              = $48
  56. Const VK_I                                              = $49
  57. Const VK_J                                              = $4A
  58. Const VK_K                                              = $4B
  59. Const VK_L                                              = $4C
  60. Const VK_M                                              = $4D
  61. Const VK_N                                              = $4E
  62. Const VK_O                                              = $4F
  63. Const VK_P                                              = $50
  64. Const VK_Q                                              = $51
  65. Const VK_R                                              = $52
  66. Const VK_S                                              = $53
  67. Const VK_T                                              = $54
  68. Const VK_U                                              = $55
  69. Const VK_V                                              = $56
  70. Const VK_W                                              = $57
  71. Const VK_X                                              = $58
  72. Const VK_Y                                              = $59
  73. Const VK_Z                                              = $5A
  74. Const VK_LEFTWIN                                = $5B   ; Left Windows key
  75. Const VK_RIGHTWIN                               = $5C   ; Right Windows key
  76. Const VK_NUMPAD0                                = $60
  77. Const VK_NUMPAD1                                = $61
  78. Const VK_NUMPAD2                                = $62
  79. Const VK_NUMPAD3                                = $63
  80. Const VK_NUMPAD4                                = $64
  81. Const VK_NUMPAD5                                = $65
  82. Const VK_NUMPAD6                                = $66
  83. Const VK_NUMPAD7                                = $67
  84. Const VK_NUMPAD8                                = $68
  85. Const VK_NUMPAD9                                = $69
  86. Const VK_MULTIPLY                               = $6A   ; Keypad *
  87. Const VK_ADD                                    = $6B   ; Keypad +
  88. Const VK_SEPARATOR                              = $6C   ; Keypad /
  89. Const VK_SUBTRACT                               = $6D   ; Keypad -
  90. Const VK_DECIMAL                                = $6E   ; Keypad .
  91. Const VK_DIVIDE                                 = $6F   ; Keypad
  92. Const VK_F1                                     = $70
  93. Const VK_F2                                     = $71
  94. Const VK_F3                                     = $72
  95. Const VK_F4                                     = $73
  96. Const VK_F5                                     = $74
  97. Const VK_F6                                     = $75
  98. Const VK_F7                                     = $76
  99. Const VK_F8                                     = $77
  100. Const VK_F9                                     = $78
  101. Const VK_F10                                    = $79
  102. Const VK_F11                                    = $7A
  103. Const VK_F12                                    = $7B
  104. Const VK_NUMLOCK                                = $90  
  105. Const VK_SCROLLLOCK                     = $91  
  106. Const VK_LEFTSHIFT                              = $A0   ; Windows 95 does not support the left and right distinguishing constants.     
  107. Const VK_RIGHTSHIFT                             = $A1  
  108. Const VK_LEFTCTRL                               = $A2  
  109. Const VK_RIGHTCTRL                              = $A3  
  110. Const VK_LEFTALT                                = $A4  
  111. Const VK_RIGHTALT                               = $A5
  112.  
  113. ; Less common keys:
  114. ; Some of the keys here would seem to be common keys, but the documentation does not say they work in 95/98,
  115. ; so I did not include them with the other keys above just to be safe.
  116. Const VK_CANCEL                                 = $03   ; Control-break processing
  117. Const VK_X1MOUSE                                = $05   ; Windows 2000/XP: X1 mouse button
  118. Const VK_X2MOUSE                                = $06   ; Windows 2000/XP: X2 mouse button
  119. Const VK_CLEAR                                  = $0C   ; CLEAR key
  120. Const VK_SELECT                                 = $29   ; SELECT key
  121. Const VK_EXECUTE                                = $2B   ; EXECUTE key
  122. Const VK_PRINT                                  = $2A   ; PRINT key
  123. Const VK_HELP                                   = $2F   ; HELP key
  124. Const VK_KANA                                   = $15   ; Input Method Editor (IME) Kana mode
  125. Const VK_HANGUL                                 = $15   ; IME Hangul mode
  126. Const VK_JUNJA                                  = $17   ; IME Junja mode
  127. Const VK_FINAL                                  = $18   ; IME final mode
  128. Const VK_KANJI                                  = $19   ; IME Hanja/Kanji mode 
  129. Const VK_CONVERT                                = $1C   ; IME convert
  130. Const VK_NONCONVERT                             = $1D   ; IME nonconvert
  131. Const VK_ACCEPT                                 = $1E   ; IME accept
  132. Const CK_MODECHANGE                             = $1F   ; IME mode change request
  133. Const VK_APPS                                   = $5D   ; Applications key
  134. Const VK_SLEEP                                  = $5F   ; Computer Sleep key
  135. Const VK_F13                                    = $7C
  136. Const VK_F14                                    = $7D
  137. Const VK_F15                                    = $7E
  138. Const VK_F16                                    = $7F
  139. Const VK_F17                                    = $80
  140. Const VK_F18                                    = $81
  141. Const VK_F19                                    = $82
  142. Const VK_F20                                    = $83
  143. Const VK_F21                                    = $84
  144. Const VK_F22                                    = $85
  145. Const VK_F23                                    = $86
  146. Const VK_F24                                    = $87
  147. Const VK_BROWSER_BACK                   = $A6   ; Windows 2000/XP: Browser Back key
  148. Const VK_BROWSER_FORWARD                = $A7   ; Windows 2000/XP: Browser Forward key
  149. Const VK_BROWSER_REFRESH                = $A8   ; Windows 2000/XP: Browser Refresh key
  150. Const VK_BROWSER_STOP                   = $A9   ; Windows 2000/XP: Browser Stop key
  151. Const VK_BROWSER_SEARCH                 = $AA   ; Windows 2000/XP: Browser Search key
  152. Const VK_BROWSER_FAVORITES              = $AB   ; Windows 2000/XP: Browser Favorites key
  153. Const VK_BROWSER_HOME                   = $AC   ; Windows 2000/XP: Browser Start And Home key
  154. Const VK_VOLUME_MUTE                    = $AD   ; Windows 2000/XP: Volume Mute key
  155. Const VK_VOLUME_DOWN                    = $AE   ; Windows 2000/XP: Volume Down key
  156. Const VK_VOLUME_UP                              = $AF   ; Windows 2000/XP: Volume Up key
  157. Const VK_MEDIA_NEXT_TRACK               = $B0   ; Windows 2000/XP: Next Track key
  158. Const VK_MEDIA_PREV_TRACK               = $B1   ; Windows 2000/XP: Previous Track key
  159. Const VK_MEDIA_STOP                     = $B2   ; Windows 2000/XP: Stop Media key
  160. Const VK_MEDIA_PLAY_PAUSE               = $B3   ; Windows 2000/XP: Play/Pause Media key
  161. Const VK_LAUNCH_MAIL                    = $B4   ; Windows 2000/XP: Start Mail key
  162. Const VK_LAUNCH_MEDIA_SELECT    = $B5   ; Windows 2000/XP: Select Media key
  163. Const VK_LAUNCH_APP1                    = $B6   ; Windows 2000/XP: Start Application 1 key
  164. Const VK_LAUNCH_APP2                    = $B7   ; Windows 2000/XP: Start Application 2 key     
  165. Const VK_OEM_1                                  = $BA   ; Windows 2000/XP: For the US standard keyboard, the ';:' key
  166. Const VK_OEM_PLUS                               = $BB   ; Windows 2000/XP: For any country/region, the '+' key
  167. Const VK_OEM_COMMA                              = $BC   ; Windows 2000/XP: For any country/region, the ',' key
  168. Const VK_OEM_MINUS                              = $BD   ; Windows 2000/XP: For any country/region, the '-' key
  169. Const VK_OEM_PERIOD                     = $BE   ; Windows 2000/XP: For any country/region, the '.' key
  170. Const VK_OEM_2                                  = $BF   ; Windows 2000/XP: For the US standard keyboard, the '/?' key
  171. Const VK_OEM_3                                  = $C0   ; Windows 2000/XP: For the US standard keyboard, the '`~' key
  172. Const VK_OEM_4                                  = $DB   ; Windows 2000/XP: For the US standard keyboard, the '[{' key
  173. Const VK_OEM_5                                  = $DC   ; Windows 2000/XP: For the US standard keyboard, the '|' key
  174. Const VK_OEM_6                                  = $DD   ; Windows 2000/XP: For the US standard keyboard, the ']}' key
  175. Const VK_OEM_7                                  = $DE   ; Windows 2000/XP: For the US standard keyboard, the 'single-quote/double-quote' key
  176. Const VK_OEM_8                                  = $DF   ; Used for miscellaneous characters; it can vary by keyboard.
  177. Const VK_ICO_F17                                = $E0  
  178. Const VK_ICO_F18                                = $E1  
  179. Const VK_OEM102                                 = $E2   ; Windows 2000/XP: Either the angle bracket key or the backslash key on the RT 102-key keyboard
  180. Const VK_ICO_HELP                               = $E3
  181. Const VK_ICO_00                                 = $E4  
  182. Const VK_PROCESSKEY                             = $E5   ; Windows 95/98/Me, Windows NT 4.0, Windows 2000/XP: IME PROCESS key
  183. Const VK_ICO_CLEAR                              = $E6
  184. Const VK_PACKET                                 = $E7   ; Windows 2000/XP: Used to pass Unicode characters as if they were keystrokes.
  185. Const VK_OEM_RESET                              = $E9
  186. Const VK_OEM_JUMP                               = $EA
  187. Const VK_OEM_PA1                                = $EB
  188. Const VK_OEM_PA2                                = $EC
  189. Const VK_OEM_PA3                                = $ED
  190. Const VK_OEM_WSCTRL                             = $EE
  191. Const VK_OEM_CUSEL                              = $EF
  192. Const VK_OEM_ATTN                               = $F0
  193. Const VK_OEM_FINNISH                    = $F1
  194. Const VK_OEM_COPY                               = $F2
  195. Const VK_OEM_AUTO                               = $F3
  196. Const VK_OEM_ENLW                               = $F4
  197. Const VK_OEM_BACKTAB                    = $F5
  198. Const VK_ATTN                                   = $F6   ; Attn key
  199. Const VK_CRSEL                                  = $F7   ; CrSel key
  200. Const VK_EXSEL                                  = $F8   ; ExSel key
  201. Const VK_EREOF                                  = $F9   ; Erase EOF key
  202. Const VK_PLAY                                   = $FA   ; Play key
  203. Const VK_ZOOM                                   = $FB   ; Zoom key
  204. Const VK_NONAME                                 = $FC   ; Reserved
  205. Const VK_PA1                                    = $FD   ; PA1 key
  206. Const VK_OEM_CLEAR                              = $FE   ; Clear key
  207.  
  208.  
  209. ; -------------------------------------------------------------------------------------------------------------------------------------
  210. ; This function returns 1 if the specified virtual key is being pressed, and 0 if it is not.
  211. ;
  212. ; Normally, the function checks to see if your application has focus and ignores any keys being held down if it does not,
  213. ; but if you want it to tell you if a key is pressed regardless of whether your app has focus, you can set IgnoreFocus to True.
  214. ; -------------------------------------------------------------------------------------------------------------------------------------
  215. Function GDI_VKeyDown(VirtualKey, IgnoreFocus=False)
  216.        
  217.         If (IgnoreFocus = True) And (GetActiveWindow() = 0) Then Return
  218.         Return (GetAsyncKeyState%(VirtualKey) And %1000000000000000) > 0
  219.        
  220. End Function


Comments :


em22(Posted 1+ years ago)

 Brilliant! been looking for something like this for a while!


_PJ_(Posted 1+ years ago)

 Excellent!A single function thhat can check mouseclicks AND keypresses :)Great stuff, Sswifty!


_PJ_(Posted 1+ years ago)

 <div class="quote"> Return (GetAsyncKeyState%(VirtualKey) And %1000000000000000) > 0 </div>I'm assuming this allows for multiple simutaneous keypresses


AJ00200(Posted 1+ years ago)

 Does it detect keypresses while the window isnt in focus?Is there a way to do this?


N(Posted 1+ years ago)

 No.  Why would you do that?


blade007(Posted 1+ years ago)

 <div class="quote"> AJ00200 wrote...Does it detect keypresses while the window isnt in focus?Is there a way to do this? </div>yes, just change: Function GDI_VKeyDown(VirtualKey, IgnoreFocus=False)to Function GDI_VKeyDown(VirtualKey, IgnoreFocus=True)


RifRaf(Posted 1+ years ago)

 is there any way to tell windows to flush the keys for all windows.. or even a specific one? Flushkeys() in blitz only flush for active window.. so if a user clicks off the blitz app the flushkeys command will not effect the blitz app.


_PJ_(Posted 1+ years ago)

 <div class="quote"> is there any way to tell windows to flush the keys for all windows.. or even a specific one? Flushkeys() in blitz only flush for active window.. so if a user clicks off the blitz app the flushkeys command will not effect the blitz app.  </div>I suppose a workaround could be to check if Blitz window loses focus (Ive seen those in code archives) and automatically flushkeys() on the blitz app then, so it's ready and 'flushed' if/when focus returns . . .?


Warner(Posted 1+ years ago)

 But if you use GetASyncKeyState, you don't need FlushKeys?


RifRaf(Posted 1+ years ago)

 Warner, The Blitz Flushkeys() after focus loss does not appear to work at all, ive tried that.  I need to send the keyup to the window, it remembers the keydown and keeps moving the player. From what I can tell the above only lets you read states.. no reset them or change them in any way ?


Warner(Posted 1+ years ago)

 Exactly, it will tell you the state of the button, regardless if the window has focus or not. So you would have to replace your KeyDown calls with the GDI_VKeyDown function above. A simple search&replace could do that, if the keycodes weren't different. :/