如何处理WndProc中char的key_down和key_up?

How to handle key_down and key_up of char in WndProc?

我正在尝试处理箭头键和 AWSD 字符的向上键和向下键消息。

我可以使用 WM_KEYUPWM_KEYDOWN 来处理箭头键的上下行为。

但是当我尝试处理上述角色的上下行为时,它无法正常工作。

我尝试 WM_KEYUPWM_KEYDOWN 来处理字符。但是在我将它们更改为 WM_CHARWM_DEADCHAR 之后。但是还是不行。

对于字符,我可以处理前几个键的按下和向上消息,然后程序不处理这些。 但是对于箭头键,程序可以正常工作。

我是新手,没有找到如何处理角色的上下行为。

下面是我试过的最后一个代码(只是 WndProc 函数)。

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    switch (uMsg)
    {
    case WM_CREATE:
        Hmainbmp = CreateWindowEx(0, "STATIC", "", WS_CHILD | WS_VISIBLE | SS_BITMAP | WS_THICKFRAME, 1, 23, WINDOW_WIDTH, WINDOW_HEIGHT, hWnd, nullptr, hInst, nullptr);
        start_game();
        break;

    case WM_CHAR:
        PRESSED_KEY = (int)wParam;
        if (PRESSED_KEY == 'A' or PRESSED_KEY == 'a') { players[1]->moves.left = true; }
        else if (PRESSED_KEY == 'D' or PRESSED_KEY == 'd') { players[1]->moves.right = true; }
        else if (PRESSED_KEY == 'W' or PRESSED_KEY == 'w') { players[1]->moves.up = true; }
        else if (PRESSED_KEY == 'S' or PRESSED_KEY == 's') { players[1]->moves.down = true; }
        break;

    case WM_DEADCHAR:
        PRESSED_KEY = (int)wParam;
        if (PRESSED_KEY == 'A' or PRESSED_KEY == 'a') { players[1]->moves.left = false; }
        else if (PRESSED_KEY == 'D' or PRESSED_KEY == 'd') { players[1]->moves.right = false; }
        else if (PRESSED_KEY == 'W' or PRESSED_KEY == 'w') { players[1]->moves.up = false; }
        else if (PRESSED_KEY == 'S' or PRESSED_KEY == 's') { players[1]->moves.down = false; }
        break;

    case WM_KEYDOWN:
        PRESSED_KEY = (int)wParam;
        if      (PRESSED_KEY == VK_LEFT)    { players[0]->moves.left    = true; }
        else if (PRESSED_KEY == VK_RIGHT)   { players[0]->moves.right   = true; }
        else if (PRESSED_KEY == VK_UP)      { players[0]->moves.up      = true; }
        else if (PRESSED_KEY == VK_DOWN)    { players[0]->moves.down    = true; }
        break;
    case WM_KEYUP:
        RELEASED_KEY = (int)wParam;
        if      (RELEASED_KEY == VK_LEFT)   { players[0]->moves.left    = false; }
        else if (RELEASED_KEY == VK_RIGHT)  { players[0]->moves.right   = false; }
        else if (RELEASED_KEY == VK_UP)     { players[0]->moves.up      = false; }
        else if (RELEASED_KEY == VK_DOWN)   { players[0]->moves.down    = false; }
        break;
    
    case WM_DESTROY:
        PostQuitMessage(0);
        break;

    default:
        return(DefWindowProc(hWnd, uMsg, wParam, lParam));
    }

    return(0L);
}

start_game() 是我的主要功能,我在该功能中使用按键的向上和向下消息。

我的问题是,如何处理 AD 等字符的按键和按键行为?


在我按下 AW 之后,程序无法处理这些字符的按键行为以及任何其他字符的按键行为,如 SD .但同时程序可以处理箭头键的上下行为。


FULL CODE (Vs2019)

根据WM_DEADCHAR

WM_DEADCHAR specifies a character code generated by a dead key. A dead key is a key that generates a character, such as the umlaut (double-dot), that is combined with another character to form a composite character.

所以 WM_DEADCHAR 消息不是你认为对应的 WM_CHAR 消息。

您还可以使用WM_KEYDOWN/WM_KEYUP消息来处理字符键:

case WM_KEYDOWN:
    PRESSED_KEY = (int)wParam;
    if      (PRESSED_KEY == VK_LEFT)    { players[0]->moves.left    = true; }
    else if (PRESSED_KEY == VK_RIGHT)   { players[0]->moves.right   = true; }
    else if (PRESSED_KEY == VK_UP)      { players[0]->moves.up      = true; }
    else if (PRESSED_KEY == VK_DOWN)    { players[0]->moves.down    = true; }
    if (PRESSED_KEY == 'A' or PRESSED_KEY == 'a') { players[1]->moves.left = true; }
    else if (PRESSED_KEY == 'D' or PRESSED_KEY == 'd') { players[1]->moves.right = true; }
    else if (PRESSED_KEY == 'W' or PRESSED_KEY == 'w') { players[1]->moves.up = true; }
    else if (PRESSED_KEY == 'S' or PRESSED_KEY == 's') { players[1]->moves.down = true; }
    break;
case WM_KEYUP:
    RELEASED_KEY = (int)wParam;
    if      (RELEASED_KEY == VK_LEFT)   { players[0]->moves.left    = false; }
    else if (RELEASED_KEY == VK_RIGHT)  { players[0]->moves.right   = false; }
    else if (RELEASED_KEY == VK_UP)     { players[0]->moves.up      = false; }
    else if (RELEASED_KEY == VK_DOWN)   { players[0]->moves.down    = false; }
    if (RELEASED_KEY == 'A' or RELEASED_KEY == 'a') { players[1]->moves.left = false; }
    else if (RELEASED_KEY == 'D' or RELEASED_KEY == 'd') { players[1]->moves.right = false; }
    else if (RELEASED_KEY == 'W' or RELEASED_KEY == 'w') { players[1]->moves.up = false; }
    else if (RELEASED_KEY == 'S' or RELEASED_KEY == 's') { players[1]->moves.down = false; }
    break;