我正在尝试复制一些十六进制编辑器的行为,该编辑器分别处理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()。所以我尝试处理2条不同的消息,其中一条跳过了另一条...

消息循环如下所示:

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继续之前,我应该添加什么?

最佳答案

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

while (GetMessage(&msg, 0, 0, 0) > 0)
{
    TranslateMessage(&msg); // here
    DispatchMessage(&msg);
}


除其他外,TranslateMessage负责从WM_CHAR生成WM_KEYDOWN消息。

关于c - WM_KEYDOWN到达,WM_CHAR被跳过,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26090060/

10-11 00:23