[英]detecting escape keypress in console mfc program
I am writing a small program in Visual Studio 11 (beta) that a console mfc app.我正在 Visual Studio 11(测试版)中编写一个小程序,它是一个控制台 mfc 应用程序。 I have never programmed for this environment before and I am trying to figure out how to detect the Escape-key press in the console.
我以前从未为这种环境编程过,我正在尝试弄清楚如何检测控制台中的 Escape 键按下。 I have looked extensively at online resources and tried many different things can't figure it out.
我广泛查看了在线资源并尝试了很多不同的东西无法弄清楚。
Here is what I was trying to see if I could get it to work.这是我试图查看是否可以让它工作的内容。
printf("Press Escape to exit.");
bool maxReached = true;
while (maxReached)
{
if(WM_COMMAND == IDCANCEL) // Tried many different things here, like WM_KEYDOWN == VK_ESCAPE. no luck
{
maxReached = false;
}
}
exit(-1);
Any help would be much appreciated.任何帮助将非常感激。
Kamal卡马尔
You can detect using您可以检测使用
if (getch() == 0x1B) // escape detected
{
maxReached = false;
}
You can always install a keyboard hook and check for the ESC key being pressed.您始终可以安装键盘挂钩并检查是否按下了 ESC 键。
Here's how:这是如何做:
First, install the hook一、安装挂钩
HHOOK hhkLowLevelKybd;
HINSTANCE hInstance = GetModuleHandle(NULL);
// Install the low-level keyboard hook
hhkLowLevelKybd = SetWindowsHookEx(WH_KEYBOARD_LL,
LowLevelKeyboardProc,
hInstance,
NULL );
Next, have LowLevelKeyboardProc execute your routines in response to the ESC keyup:接下来,让 LowLevelKeyboardProc 执行您的例程以响应 ESC keyup:
_declspec(dllexport) LRESULT CALLBACK LowLevelKeyboardProc( int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode == HC_ACTION)
{
PKBDLLHOOKSTRUCT p = ( PKBDLLHOOKSTRUCT ) lParam;
switch (wParam)
{
case WM_KEYUP:
case WM_SYSKEYUP:
switch (p->vkCode)
{
case 0x1B: //OR VK_ESCAPE
maxReached = false;
break;
default:
break;
}
break;
default:
break;
}
}
return(CallNextHookEx( NULL, nCode, wParam, lParam ));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.