简体   繁体   English

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

[英]How to handle key_down and key_up of char in WndProc?

I'm trying to handle key up and key down messages for arrow keys and A , W , S , D characters.我正在尝试处理箭头键和AWSD字符的向上和向下键消息。

I can handle arrow keys' up and down behavior using WM_KEYUP and WM_KEYDOWN .我可以使用WM_KEYUPWM_KEYDOWN处理箭头键的上下行为。

But when I try to handle above characters' up and down behavior, it doesn't work properly.但是当我尝试处理上述角色的上下行为时,它就无法正常工作。

I tried WM_KEYUP and WM_KEYDOWN to handle chracters.我试过WM_KEYUPWM_KEYDOWN来处理字符。 But after I changed them to WM_CHAR and WM_DEADCHAR .但是在我将它们更改为WM_CHARWM_DEADCHAR之后。 But it still doesn't work.但它仍然不起作用。

For characters I can handle first few key down and up messages, then program doesn't handle these.对于字符,我可以处理前几个按键向下和向上消息,然后程序不处理这些。 But for arrow keys, program works properly.但是对于箭头键,程序可以正常工作。

I'm new in this, didn't found how to handle character's up and down behaviors.我是新手,没有找到如何处理角色的上下行为。

Below is the last code (Just WndProc function)I tried.下面是我试过的最后一个代码(只是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() is my main function, I'm using key's up and down messages in that function. start_game()是我的主要 function,我在 function 中使用键的向上和向下消息。

My question is, how can I handle key down and key up behaviors of characters such as A , D ?我的问题是,如何处理AD等字符的按键和按键行为?


After I press to A or W program can't handle key up behavior of these characters and any other character's key down behavior like S and D .在我按下AW后,程序无法处理这些字符的按键行为以及任何其他字符的按键行为,例如SD But at the same time program can handle arrow keys' up and down behavior.但同时程序可以处理箭头键的上下行为。


FULL CODE (Vs2019)完整代码(Vs2019)

According to WM_DEADCHAR :根据WM_DEADCHAR

WM_DEADCHAR specifies a character code generated by a dead key. WM_DEADCHAR指定由死键生成的字符代码。 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.死键是生成字符的键,例如变音符号(双点),它与另一个字符组合以形成复合字符。

So the WM_DEADCHAR message is not what you think corresponds to the WM_CHAR message.所以WM_DEADCHAR消息不是你认为对应于WM_CHAR消息的。

You can also use WM_KEYDOWN / WM_KEYUP messages to handle character keys:您还可以使用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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM