[英]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.我正在尝试处理箭头键和
A
、 W
、 S
、 D
字符的向上和向下键消息。
I can handle arrow keys' up and down behavior using WM_KEYUP
and WM_KEYDOWN
.我可以使用
WM_KEYUP
和WM_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_KEYUP
和WM_KEYDOWN
来处理字符。 But after I changed them to WM_CHAR
and WM_DEADCHAR
.但是在我将它们更改为
WM_CHAR
和WM_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
?我的问题是,如何处理
A
、 D
等字符的按键和按键行为?
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
.在我按下
A
或W
后,程序无法处理这些字符的按键行为以及任何其他字符的按键行为,例如S
和D
。 But at the same time program can handle arrow keys' up and down behavior.但同时程序可以处理箭头键的上下行为。
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.