Getting additional info about pressed keyboard or mouse keys
The GetKeyState
function retrieves the status of the specified virtual key.
The list of virtual codes is provided in Keyboard Input Virtual-Key Codes.
The sample code below shows how to distinguish between Shift+F2 and Shift+U using GetKeyState() function.
$SAMPLECODE$
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 PUBLIC oform1 oForm1=NEWOBJECT("form1") oForm1.Show() RETURN DEFINE CLASS form1 AS form Caption = "Form1" KeyPreview = .T. Name = "Form1" ADD OBJECT command1 AS commandbutton WITH ; Top = 192, Left = 145, Height = 27, Width = 84, ; Caption = "Exit", ; Name = "Command1" ADD OBJECT text1 AS textbox WITH ; Height = 23, Left = 85, Top = 84, Width = 204, ; ReadOnly = .T., ; Name = "Text1" PROCEDURE KeyPress LPARAMETERS nKeyCode, nShiftAltCtrl #DEFINE VK_F2 0x71 DECLARE Long GetKeyState IN WIN32API Long vKey IF nShiftAltCtrl = 1 AND nKeyCode = 85 && Shift+F2 or Shift+U IF GetKeyState(VK_F2) < 0 Thisform.text1.Value = "Shift+F2" ELSE Thisform.text1.Value = "Shift+U" ENDIF ELSE Thisform.text1.Value = "" ENDIF ENDPROC PROCEDURE command1.Click Thisform.Release() ENDPROC ENDDEFINE
Comments
Detect Alt key
I didn't see the code for Alt key in Keyboard Input Virtual-Key Codes topic. Can we detect that user press ALT key in a top level form or normal form?
It's VK_MENU for the ALT key
The code for ALT key is <b>VK_MENU</b> because its default Windows behavior is to activate a menu in an application. As result you cannot act on it in VFP because it activates a menu as well and doesn't trigger any events. It could be possible to hook into Windows message with Bindevent and itercept it but I never tried it.