nrpanda


Hi !

I have developed a simple form with 5 textboxes and 2 command buttons. I want to trap ALT key in the KeyPress event of TextBox or Form. I am able to trap Shift Key in KeyPress event of both TextBox and Form only. When I am putting code line {Read Event} in Form.Activate event, then I am able to trap CTRL key. But in no way I am able to trap ALT key. I am using Microsoft Visual Foxpro 6.0 version. I have msdn loaded in my machine. When I search for KeyPress event in MSDN, at the last line, it is clearly written that ALT with any key combinations cannot be traped in KeyPress Event. But I found some code snippets in MSDN using code like nShiftCtrlAlt=4 in a condition.

I want to trap actually ALT+C, ALT+A, ALT+M and ALT+X. How can I do that in KeyPress Event of TextBox or Form. Is there any other way

Please, anyone is there who can help me to fix this problem

By Nihar




Re: How can I trap ALT key in KeyPress Event of TextBox or Form ?

CetinBasoz


As far as I know you can't trap them directly in Keypress. In newer versions there are accelarator key definitions for captions like label and commandbutton captions ( for example if you set label caption to "\<My Caption" then Alt+M sets the focus to label, but since labels cannot take focus you can use that trick to set to focus to the Textbox that has taborder next to that label ).

Alt+Key is reserved for menus so one workaround is to define a menu with some bars using those key shortcuts. You don't need to activate the menu and it works very well.






Re: How can I trap ALT key in KeyPress Event of TextBox or Form ?

dni

Yoy may find at:

https://msdn2.microsoft.com/en-us/library/87b17kzd(VS.80).aspx

You may need to put property KeyPreview = .T. on the form, and put your "trap" code in KeyPress method, unde parameters.







Re: How can I trap ALT key in KeyPress Event of TextBox or Form ?

nrpanda

Dear dni,

Sorry !

Its not working. I have already checked it(Form.KeyPreview=.t.) b4 posting my question. Also i have tried it through laskey() function to trap ALT key press in KeyPress event of form as well as ogf TextBox. Its also not working in case case of ALT key.

I wrote the code in KeyPress event as :

{

messagebox("nShiftAltCtrl : "+str(nShiftAltCtrl))

messagebox("last key : "+str(lastkey())

}

You will find it strange to read. I have 5 textboxes in ascending tab index order. I have set accesskey "C" to Cancel button and "M" to Modify button. I can trap ALT+C and ALT+M when focus is in last 4 textboxes, but it is not excuting the code written in KeyPress event of TextBox or Form, rather the of Cancel button for example, exactly what i want.

Unfortunately, its not working same in Text1(first textbox). I want to excute code of respective command buttons if pressed with their accesskey char combined with ALT key from TextBox1 too. I cannot find out the reason.

Please tell me, if there is any other way to trap.

Thanks 4 ur reply.

Re: Nihar





Re: How can I trap ALT key in KeyPress Event of TextBox or Form ?

CetinBasoz

Aren't you reading half of the messages





Re: How can I trap ALT key in KeyPress Event of TextBox or Form ?

Naomi Nosonovsky

Check solution by Fabio Lunardon:

PUBLIC oform1

oform1=NEWOBJECT("form1")
ACTIVATE WINDOW "Debug Output"
oform1.Show

RETURN

DEFINE CLASS form1 AS form
	Autocenter = .t.
	showwindow = 2
	Caption = "IMPLEMENT KeyUp and KeyDown events"
	Name = "form1"
	keypreview = .T.

	ADD OBJECT text1 AS textbox WITH ;
		Height = 23, ;
		Left = 92, ;
		Top = 80, ;
		Width = 100, ;
		Name = "Text1"

	PROCEDURE keypress
		LPARAMETERS nKeyCode, nShiftAltCtrl
		DEBUGOUT "Form", nKeyCode, nShiftAltCtrl

	PROCEDURE keyup
		LPARAMETERS nVirtualKeyCode;
		,	nScanCode		;
		,	lExtendKey

		DEBUGOUT "Up ",nVirtualKeyCode;
		,	nScanCode		;
		,	lExtendKey
	ENDPROC


	PROCEDURE keydown
		LPARAMETERS nVirtualKeyCode;
		,	nScanCode		;
		,	lExtendKey		;
		,	lStillDown		;
		,	nRepeatCount

		DEBUGOUT "Down",nVirtualKeyCode;
		,	nScanCode		;
		,	lExtendKey		;
		,	lStillDown		;
		,	nRepeatCount
	ENDPROC


	PROCEDURE winproc
		LPARAMETERS hWnd,msg,wParam,lParam

		#DEFINE WM_KEYDOWN					0x0100
		#DEFINE WM_KEYUP					0x0101

		DO CASE
			CASE m.msg = WM_KEYDOWN
				this.KeyDown(m.wParam,	BITAND(BITRSHIFT(m.lParam,16),0xFF);
						,	BITTEST(m.lParam,24)	;
						,	BITTEST(m.lParam,30)	;
						,	BITAND(m.lParam,0xFFFF))
			CASE m.msg = WM_KEYUP
				this.KeyUp(m.wParam,	BITAND(BITRSHIFT(m.lParam,16),0xFF);
						,	BITTEST(m.lParam,24))
		ENDCASE

		RETURN CallWindowProc(m.this.WndProc,m.hWnd,m.msg,m.wParam,m.lParam)
	ENDPROC


	PROCEDURE Init
		#DEFINE WM_KEYDOWN					0x0100
		#DEFINE WM_KEYUP					0x0101
		#DEFINE	GWL_WNDPROC					-4

		DECLARE LONG		GetWindowLong	IN user32.dll	LONG	,INTEGER nIndex 
		DECLARE integer		CallWindowProc	IN user32.dll	;
             integer lpPrevWndFunc,LONG , integer Msg ,integer wParam ,integer lParam

		ADDPROPERTY(M.THIS,'WndProc',GetWindowLong(m.this.hWnd,GWL_WNDPROC))

		CLEAR DLLS "GetWindowLong"

		=BINDEVENT(m.this.hWnd,WM_KEYDOWN,m.this,'WinProc');
		,BINDEVENT(m.this.hWnd,WM_KEYUP,m.this,'WinProc')
	ENDPROC

	PROCEDURE Destroy
		CLEAR DLLS "CallWindowProc"
ENDDEFINE

For the ALT, and others system/hot keys, WM_SYSKEYDOWN,WM_SYSKEYUP ...
can to be used, but the control is more complex.





Re: How can I trap ALT key in KeyPress Event of TextBox or Form ?

dni

If you like to take in account the link and what Cetin said, it should work.






Re: How can I trap ALT key in KeyPress Event of TextBox or Form ?

Tamar E. Granor

FWIW, we've had accelerator keys for captions since VFP 3.

Tamar