开发者

How to handle Ctrl+X shortcut keys

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:
            开发者_StackOverflow社区    MessageBox(0, "Enter", "Key", 0);
                break;
        }
    }
    break;


Rather than handle low-level keypress events, it is best to handle such keyboard actions with accelerators.


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 (eg OutputDebugString()) that's not going to interfere with the input.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜