Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Detecting the shift state of keys in realtime
Message
From
07/02/2019 10:16:09
 
 
To
07/02/2019 08:48:11
General information
Forum:
Visual FoxPro
Category:
Windows API functions
Environment versions
Visual FoxPro:
VFP 9 SP2
OS:
Windows 10
Miscellaneous
Thread ID:
01665968
Message ID:
01666024
Views:
47
>>>>Me again,
>>>>
>>>>I have been playing around with the WinApi's getKeyState and GetKeyboardState function to determine when the Shift, Ctrl, or Alt key are pressed when a routine occurs. But the results tend to be random. Does anyone have a solution that returns the state of these keys? FYI. the routine is occurring in a TImer. I am attempt to implement a more dynamic tooltip that would display the content based on the state of the shift state and the content the mouse happens to be over at the time.
>>>>
>>>>Any suggestion are welcome.
>>>
>>>Use GetAsyncKeyState(). It returns a 32-bit value that can have the lower 16-bits parsed using BITAND(lnResult, 0x8000) to determine if the key is down or up (non-zero = down, zero = up).
>>>
>>>You can see all of the virtual keys to test, including VK_SHIFT, VK_CONTROL, VK_MENU (alt), as well as VK_RSHIFT, VK_LSHIFT, etc.
>>>
>>>
#define key_lshift 0xa0
>>>#define key_rshift 0xa1
>>>#define key_lctrl 0xa2
>>>#define key_rctrl 0xa3
>>>#define key_lalt 0xa4
>>>#define key_ralt 0xa5
>>>
>>>DECLARE INTEGER GetAsyncKeyState IN WIN32API INTEGER nVKey
>>>
>>>LOCAL llLShiftDown, llRShiftDown, llLCtrlDown, llRCtrlDown, llLAltDown, llRAltDown
>>>llLShiftDown = isKeyDown(key_lshift)
>>>llRShiftDown = isKeyDown(key_rshift)
>>>llLCtrlDown  = isKeyDown(key_lctrl)
>>>llRCtrlDown  = isKeyDown(key_rctrl)
>>>llLAltDown   = isKeyDown(key_lalt)
>>>llRAltDown   = isKeyDown(key_ralt)
>>>
>>>* The same works for reading mouse buttons
>>>#define left_button 0x1
>>>#define right_button 0x2
>>>#define middle_button 0x4
>>>llLeftButtonDown   = isKeyDown(left_button)
>>>llRightButtonDown  = isKeyDown(right_button)
>>>llMiddleButtonDown = isKeyDown(middle_button)
>>>
>>>
>>>FUNCTION isKeyDown
>>>LPARAMETERS tnVKey
>>>    RETURN (BITAND(GetAsyncKeyState(tnVKey), 0x8000) != 0)
>>>
>>>You can also BINDEVENTS(thisForm.HWnd, WM_KEYDOWN, "thisForm", "my_method") and intercept keystrokes which are made as they are made. This will be for any keystroke directed to that window. See WM_KEYDOWN for the bits to examine there to retrieve the vKey and live state as it changes.
>>
>>More good suggestions. Thanks. Fortunately my needs doesn't need to distinguish the Left/Right key or whether the key is Up or Down. Another tool for my toolbox. (can't have too many tools)
>
>In that case, just use the un-branded forms:
>
>#define key_shift 0x10
>#define key_ctrl 0x11
>#define key_alt 0x12
>
>Those will tell you if either shift, ctrl, or alt key is pressed. This link has a list of all the virtual key codes. You can find every key on the keyboard there, plus things like mouse and special media buttons, allowing you to write a VFP app that responds to the play, stop, prev, next, volume up, volume down, etc., buttons on a multi-media keyboard.

Ok, I have these defined in a .H file, for the purpose of the example I added, I changed them to their hex values.
Greg Reichert
Previous
Reply
Map
View

Click here to load this message in the networking platform