Click to See Complete Forum and Search --> : How do I implement the KeyPreview function VB has in VC++ 6.0


Jason Watson
April 23rd, 1999, 12:09 AM
I want to Emulate the KeyPreview facility that VB has. ie In a dialog box I want to process any keypresses in the dialog box before they go to the controls in the dialog box. I'm using VC++ 6.0

Help....

Centurion Software Australia

Jason Teagle
April 23rd, 1999, 02:33 AM
Override the dialogue box's PreTranslateMessage() method as follows:

---
BOOL CMyDialogue::PreTranslateMessage(MSG *psMSG)
{
BOOL bDealtWith = FALSE ;

if (psMSG->message == WM_CHAR)
{
// If you want to call the default route for a keypress as if it
// had gone to the window as normal, use:
OnChar( (UINT)psMSG->wParam,(UINT)LOWORD(psMSG->lParam),
(UINT)HIWORD(psMSG->lParam));

// Or, if you want to know when the key was dealt with (i.e.,
// does not get passed on to rest of command chain if dealt with),
// use:
bDealtWith = MyKeyPreview( (UINT)psMSG->wParam);
// This routine would return TRUE if it handled the key and should
// NOT be passed on, or FALSE if it ignored the key and should let
// the system handle it as normal.
}

return bDealtWith ;
}


---

You may also want to trap WM_SYSCHAR for <Alt>key combos, and also WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, WM_SYSKEYUP to trap non-ASCII-generating keys.

Does this help?