Win32 capture Escape and Enter key window message.
-
Good day. I have a question about how to capture escape and enter key window message in Win32. Normally in MFC, I will use PreTranslateMessage like below to capture enter and escape key...
BOOL CTestDlg::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_KEYDOWN) {
if (pMsg->wParam==VK_ESCAPE || pMsg->wParam==VK_RETURN || pMsg->wParam==VK_CANCEL) {
return FALSE;
}
}return CDialog::PreTranslateMessage(pMsg);
}
But somehow it seems that enter key and escape key cannot be captured in Win32 when I waiting for WM_KEYDOWN window message like below:
int CALLBACK TestDlgProc(HWND hDlg, UINT Message, WPARAM wParam, LPARAM lParam)
{
switch (Message) {
case WM_KEYDOWN:
switch (wParam) {
case VK_ESCAPE:
MessageBox(NULL, "VK_ESCAPE", "WM_KEYDOWN", MB_OK);
break;case VK\_RETURN: MessageBox(NULL, "VK\_RETURN", "WM\_KEYDOWN", MB\_OK); break; } break; }
}
Please help me. Is there anything I haven't done to make my code work?
-
Good day. I have a question about how to capture escape and enter key window message in Win32. Normally in MFC, I will use PreTranslateMessage like below to capture enter and escape key...
BOOL CTestDlg::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_KEYDOWN) {
if (pMsg->wParam==VK_ESCAPE || pMsg->wParam==VK_RETURN || pMsg->wParam==VK_CANCEL) {
return FALSE;
}
}return CDialog::PreTranslateMessage(pMsg);
}
But somehow it seems that enter key and escape key cannot be captured in Win32 when I waiting for WM_KEYDOWN window message like below:
int CALLBACK TestDlgProc(HWND hDlg, UINT Message, WPARAM wParam, LPARAM lParam)
{
switch (Message) {
case WM_KEYDOWN:
switch (wParam) {
case VK_ESCAPE:
MessageBox(NULL, "VK_ESCAPE", "WM_KEYDOWN", MB_OK);
break;case VK\_RETURN: MessageBox(NULL, "VK\_RETURN", "WM\_KEYDOWN", MB\_OK); break; } break; }
}
Please help me. Is there anything I haven't done to make my code work?
I can suggest you 2 approach now, 1) use WM_COMMAND (wparam = IDOK for Enter key, and wparam = IDCANCEL for ESC key) and use different Dlg ID for OK and CANCEL key if your dialog has one. 2) in the main message loop put a route for your pretranslate function
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
BOOL bTranslate = TRUE;if (msg.hwnd is your dialog window) { bTranslate = pretranslate(); // get your key message } if (bTranslate) TranslateMessage(&msg); DispatchMessage(&msg); } }
I didn't tried please try and may us know
-
Good day. I have a question about how to capture escape and enter key window message in Win32. Normally in MFC, I will use PreTranslateMessage like below to capture enter and escape key...
BOOL CTestDlg::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_KEYDOWN) {
if (pMsg->wParam==VK_ESCAPE || pMsg->wParam==VK_RETURN || pMsg->wParam==VK_CANCEL) {
return FALSE;
}
}return CDialog::PreTranslateMessage(pMsg);
}
But somehow it seems that enter key and escape key cannot be captured in Win32 when I waiting for WM_KEYDOWN window message like below:
int CALLBACK TestDlgProc(HWND hDlg, UINT Message, WPARAM wParam, LPARAM lParam)
{
switch (Message) {
case WM_KEYDOWN:
switch (wParam) {
case VK_ESCAPE:
MessageBox(NULL, "VK_ESCAPE", "WM_KEYDOWN", MB_OK);
break;case VK\_RETURN: MessageBox(NULL, "VK\_RETURN", "WM\_KEYDOWN", MB\_OK); break; } break; }
}
Please help me. Is there anything I haven't done to make my code work?
Hi, You can also try SetWindowsHookEx API. ---- Catherine Sea Dynamsoft Corporation
www.dynamsoft.com the leading developer of version control and issue tracking software