如何处理Ctrl+X快捷键
当我同时按下 Ctrl+Enter 时,我收到第一个消息框,但没有收到第二个消息框。我该如何解决这个问题?
case WM_KEYDOWN:
if (GetKeyState(VK_CONTROL) & 0x8000) {
MessageBox(0, "Ctrl", "Key", 0);
switch (wParam) {
case VK_RETURN:
MessageBox(0, "Enter", "Key", 0);
break;
}
}
break;
I receive the first message box but not the second one when I press Ctrl+Enter together. How can I fix this?
case WM_KEYDOWN:
if (GetKeyState(VK_CONTROL) & 0x8000) {
MessageBox(0, "Ctrl", "Key", 0);
switch (wParam) {
case VK_RETURN:
MessageBox(0, "Enter", "Key", 0);
break;
}
}
break;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最好使用加速器处理此类键盘操作,而不是处理低级按键事件。
Rather than handle low-level keypress events, it is best to handle such keyboard actions with accelerators.
加速器通常用于应用程序级命令 - 例如。 Ctrl-N 打开一个新文档。如果此组合键特定于此
HWND
- 例如。如果它是特定于控件的键盘命令,则在控件中处理它是正确的方法。我对代码中发生的情况的猜测如下:当您按 Ctrl+Enter 时,Windows 会生成两条
WM_KEYDOWN
消息;一个用于 CTRL,一个用于 ENTER。当您获得 CTRL 的消息时,您将显示消息框,现在其内部消息循环接管 - 它将获得任何进一步的输入,直到它被关闭。尝试删除第一个
MessageBox
(无论如何你都知道你已经到达了那个点),然后看看第二个是否被击中。或者使用一些不会干扰输入的诊断输出技术(例如OutputDebugString()
)。Accelerators are typically used for application-level commands - eg. Ctrl-N to open a new document. If this key combo is specific to this
HWND
- eg. if it is a control-specific keyboard command, then processing it in the control is the way to go.My guess as to what's happening in your code is as follows: when you hit Ctrl+Enter, Windows generates two
WM_KEYDOWN
messages; one for the CTRL, and one for the ENTER. when you get the one for the CTRL, you display the message box, and now its internal message loop takes over - it's going to get any further input until it is dismissed.Try dropping the first
MessageBox
(you know you're hitting that point anyhow), and just see if the second one gets hit. Or use some diagnostic output technique (egOutputDebugString()
) that's not going to interfere with the input.