Capture Keyboard Input
-
Can someone tell me how to capture key presses when they are being entered into a Text box. I did add the ON_WM_KEYUP and OM_WM_KEYDOWN messages but breakpoints in there procedures (OnkeyDown etc) were never reached.
Hi Sweep, One way is to use PreTranslateMessage virtual function, e.g.
BOOL CMyWindow::PreTranslateMessage(MSG* pMsg)
{
BOOL bEatMe = FALSE;if (pMsg && pMsg->message == WM\_KEYDOWN && pMsg->hwnd == m\_edit1.GetSafeHwnd()) { BOOL bShift = (GetKeyState(VK\_SHIFT) < 0); BOOL bCtrl = (GetKeyState(VK\_CONTROL) < 0); BOOL bAlt = (pMsg->message == WM\_SYSKEYDOWN); // pMsg->wParam holds the keycode... bEatMe = FALSE; // FALSE if you want the key to be processed } // return if we've processed this message or want the default/system to return (bEatMe) ? TRUE:CDialog::PreTranslateMessage(pMsg);
}
If you are trying to do clever things, my advice would be to create a class (derived from CEdit) and use this in your views/dialogs. Hope this helps, Andy
-
Can someone tell me how to capture key presses when they are being entered into a Text box. I did add the ON_WM_KEYUP and OM_WM_KEYDOWN messages but breakpoints in there procedures (OnkeyDown etc) were never reached.
Depends what you want to capture. If your only intrested in 0 > 9, a > z, A > Z plus a few others like VK_RETURN, VK_ESCAPE etc - the easist way is to add a ON_WM_CHAR handler to your text box class. Anything that has its WM_KEYUP/WM_KEYDOWN translated to a WM_CHAR will be caught this way. Though - your ON_WM_KEYUP/DOWN handlers should work. Is this text box part of a dialog? Is it being subclased correctly, probably via a DDX_Control?
-
Depends what you want to capture. If your only intrested in 0 > 9, a > z, A > Z plus a few others like VK_RETURN, VK_ESCAPE etc - the easist way is to add a ON_WM_CHAR handler to your text box class. Anything that has its WM_KEYUP/WM_KEYDOWN translated to a WM_CHAR will be caught this way. Though - your ON_WM_KEYUP/DOWN handlers should work. Is this text box part of a dialog? Is it being subclased correctly, probably via a DDX_Control?
-
The Edit box has not been sub-classed, I assume I need to do that to get the WM_KEYUP/WM_KEYDOWN working. I just ant to trap the Tab and Return keys.
lol. Would be them two wouldn't it. Well, your KEYUP/DOWN handler will and should work for all other keys. Not however, Tab and return, which are dialog box navigation keys. The system handles these in a different way "The WM_GETDLGCODE message is sent to the window procedure associated with a control. By default, the system handles all keyboard input to the control; the system interprets certain types of keyboard input as dialog box navigation keys. To override this default behavior, the control can respond to the WM_GETDLGCODE message to indicate the types of input it wants to process itself." In other words, you'll never get a KEYUP/DOWN for Tab/return as you get a WM_GETDLGCODE which returns DLGC_DEFPUSHBUTTON/DLGC_WANTTAB depending
-
lol. Would be them two wouldn't it. Well, your KEYUP/DOWN handler will and should work for all other keys. Not however, Tab and return, which are dialog box navigation keys. The system handles these in a different way "The WM_GETDLGCODE message is sent to the window procedure associated with a control. By default, the system handles all keyboard input to the control; the system interprets certain types of keyboard input as dialog box navigation keys. To override this default behavior, the control can respond to the WM_GETDLGCODE message to indicate the types of input it wants to process itself." In other words, you'll never get a KEYUP/DOWN for Tab/return as you get a WM_GETDLGCODE which returns DLGC_DEFPUSHBUTTON/DLGC_WANTTAB depending
-
So what about Andy's solution of using PreTranslateMessage, would that work? Or I am stuck!?!
What they are trying to say is some other alternatives are error prone, subclass your CEdit is better like this article http://www.codeproject.com/editctrl/NumEditCtl.asp Sonork 100.41263:Anthony_Yio
-
So what about Andy's solution of using PreTranslateMessage, would that work? Or I am stuck!?!
The solution Andy Q gave you should work with tab and enter keys. I used a similar technique to trap Enter key in a form view.
BOOL CMyFormView::PreTranslateMessage(MSG* pMsg) { // keyboard input for 'enter' and 'esc' if(pMsg->message==WM_KEYDOWN) { if(pMsg->wParam == 0x0d) // if Enter pressed { //your handler return TRUE; } else if(pMsg->wParam == 0x1b) // if Esc pressed { //... return TRUE; } } return CFormView::PreTranslateMessage(pMsg); }