Here’s another neat little MapBasic function I’ve been using recently. I needed a way of detecting whether a key on the keyboard was currently pressed or not. My intention was to show a hidden debugging dialog if a user shift-clicked on a specific button control, but there’s no in-built functions in MapBasic for detecting key states. Fortunately it’s possible to hook into the Windows User32.dll to use the GetAsyncKeyState call. Here’s how this all works in MapBasic…
First, we’ve got to define the GetASyncKeyState call and a bunch of related constants which map keys to a numeric value. Copy and paste the contents below to a .def file (or download a pre-made version later in this post).
asynckeys.def:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | Declare Function GetAsyncKeyState Lib "User32.dll" Alias "GetAsyncKeyState" (ByVal vKey As Integer) As IntegerDEFINE vbKeyShift 16DEFINE vbKey1 49DEFINE vbKey2 50DEFINE vbKey3 51DEFINE vbKey4 52DEFINE vbKey5 53DEFINE vbKey6 54DEFINE vbKey7 55DEFINE vbKey8 56DEFINE vbKey9 57DEFINE vbKey0 48DEFINE vbKeyBack 8DEFINE vbKeyTab 9DEFINE vbKeyReturn 13DEFINE vbKeyControl 17DEFINE vbKeyMenu 18DEFINE vbKeyPause 19DEFINE vbKeyEscape 27DEFINE vbKeySpace 32DEFINE vbKeyEnd 35DEFINE vbKeyHome 36DEFINE vbKeyLeft 37DEFINE vbKeyRight 39DEFINE vbKeyUp 38DEFINE vbKeyDown 40DEFINE vbKeyInsert 45DEFINE vbKeyDelete 46DEFINE vbKeyMultiply 106DEFINE vbKeyDivide 111DEFINE vbKeyAdd 107DEFINE vbKeySubtract 109DEFINE vbKeyDecimal 110DEFINE vbKeyF1 112DEFINE vbKeyF2 113DEFINE vbKeyF3 114DEFINE vbKeyF4 115DEFINE vbKeyF5 116DEFINE vbKeyF6 117DEFINE vbKeyF7 118DEFINE vbKeyF8 119DEFINE vbKeyF9 120DEFINE vbKeyF10 121DEFINE vbKeyF11 122DEFINE vbKeyF12 123DEFINE vbKeyNumlock 144DEFINE vbKeyScrollLock 145DEFINE vbKeySnapshot 44DEFINE vbKeyPageUp 33DEFINE vbKeyPageDown 34DEFINE vbKeyNumpad1 97DEFINE vbKeyNumpad2 98DEFINE vbKeyNumpad3 99DEFINE vbKeyNumpad4 100DEFINE vbKeyNumpad5 101DEFINE vbKeyNumpad6 102DEFINE vbKeyNumpad7 103DEFINE vbKeyNumpad8 104DEFINE vbKeyNumpad9 105DEFINE vbKeyNumpad0 96DEFINE vbKeyPressed -32767DEFINE vbKeyWasPressed 1 |
Now, you can test for the state of any key by calling getASyncKeyState( vKey ), where vKey is an integer value corresponding to the key you want to test. So getASyncKeyState( vbKeyControl ) will test the state of the control key. The value returned will reflect whether the key is currently pressed (vbKeyPressed), or has been pressed since the last call to the function (vbKeyWasPressed). Easy! Here’s a little sample program to demonstrate how this all works:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | Include "asynckeys.def"Declare Sub MainDeclare Sub KeyCheckSub KeyCheck ' Store the pressed state of the SHIFT key Dim iState As Integer iState = getASyncKeyState( vbKeyShift ) ' Check what the state was Do Case iState Case vbKeyPressed Print "SHIFT is being pressed!" Case vbKeyWasPressed Print "SHIFT has been pressed since last check" Case Else Print "Nothing to report..." End Case ' Keep the dialog around Dialog PreserveEnd SubSub Main Dialog Title "ASync Key State" Control OKButton Title "Check" Calling KeyCheck Control CancelButton Title "Quit"End Sub |
Both the asynckeys.def definition file and the sample program are contained in this archive.










