Getting additional info about pressed keybord 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.

This is sample code. Add error handling and adjust to your requirements as necessary.

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

Hi
I didn't see the code for Alt key in Keyboard Input Virtual-Key Codes topic.Is there a code for that we can detect the user press this key in a top level form or normal form?

Hi Ali,

The code for ALT key is VK_MENU 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.