WM_KEYDOWN到达,跳过WM_CHAR

时间:2014-09-28 21:56:18

标签: c winapi

我试图复制某个十六进制编辑器的行为,它分别处理WM_KEYDOWN和WM_CHAR,它们都按照相应的顺序到达。它看起来像这样:

case WM_CHAR:
    if(GetKeyState(VK_CONTROL) & 0x8000)return 0;
    c[0] = (char)(wParam&0xFF);
    c[1] = 0;
    InputData(c);
    return 0;

case WM_KEYDOWN:
    if(GetKeyState(VK_CONTROL) & 0x8000){
        switch(wParam){
        case 0x43: //Ctrl+C
            MemViewCallB(hMemView,WM_COMMAND,MENU_MV_EDIT_COPY,0);
            return 0;
        case 0x56: //Ctrl+V
            MemViewCallB(hMemView,WM_COMMAND,MENU_MV_EDIT_PASTE,0);
            return 0;
        case 0x5a: //Ctrl+Z
            UndoLastPatch(); break;
        case 0x41: //Ctrl+A
            // Fall through to Ctrl+G
        case 0x47: //Ctrl+G
            GotoAddress(hwnd); break;
        case 0x46: //Ctrl+F
            OpenFindDialog(); break;
        }
    }
    return 0;

我的代码是:

case WM_CHAR: {
    if (GetKeyState(VK_CONTROL) & 0x8000) return 0;
    char c[2], result = -1;
    c[0] = (char) wParam & 0xFF;
    c[1] = 0;
    if (MouseArea == TEXT) {
        //parse as a character
    } else {
        //parse as a number
        }
    }
    return 0;
}

case WM_KEYDOWN:
    if (GetKeyState(VK_CONTROL) & 0x8000) {
        switch(wParam) {
        case 0x43: // Ctrl+C
            HexEditorProc(HexEditorHWnd, WM_COMMAND, IDC_C_HEX_COPY_AUTO, 0);
            return 0;
        case 0x47: // Ctrl+G
            HexEditorProc(HexEditorHWnd, WM_COMMAND, IDC_C_HEX_GOTO, 0);
            return 0;
        }
    }
    return 0;

只有WM_KEYDOWN到达。

当然,我试图在WM_KEYDOWN中完成所有这一切,将(GetKeyState(VK_CONTROL) & 0x8000)之外的所有按下作为字符处理,使用ToUnicode()将它们转换为unicode,但是我传递给它的变量在最后被破坏了Proc。出于某种原因,没有关于如何使用它的深入教程ToUnicode()。所以我尝试了两个不同的消息,其中一个跳过另一个消息......

消息循环如下所示:

while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
{
    if (HexEditorHWnd && IsDialogMessage(HexEditorHWnd, &msg))
            // stuff?  
        continue;
    if (RamWatchHWnd && IsDialogMessage(RamWatchHWnd, &msg))
    {
        if(msg.message == WM_KEYDOWN) // send keydown messages to the dialog (for accelerators, and also needed for the Alt key to work)
            SendMessage(RamWatchHWnd, msg.message, msg.wParam, msg.lParam);
        continue;
    }
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}

在HexEditor继续之前我应该​​添加什么?

2 个答案:

答案 0 :(得分:1)

您可能没有在消息循环中调用TranslateMessage

while (GetMessage(&msg, 0, 0, 0) > 0)
{
    TranslateMessage(&msg); // here
    DispatchMessage(&msg);
}
除其他事项外,

TranslateMessage负责从WM_CHAR生成WM_KEYDOWN条消息。

答案 1 :(得分:0)

这个小小的补充似乎解决了我的问题。

if (HexEditorHWnd && IsDialogMessage(HexEditorHWnd, &msg))
{
    if(msg.message == WM_CHAR)
        SendMessage(HexEditorHWnd, msg.message, msg.wParam, msg.lParam);
    continue;
}