S
S
s2sk13372017-09-25 21:25:38
C++ / C#
s2sk1337, 2017-09-25 21:25:38

How to catch pressing of two WinApi keys?

Hello.
I can not catch up with how to catch pressing two keys at the same time. I need the action to be performed exactly NOT when two of these keys are pressed, and if one key is pressed, then perform such and such an action and if the second one still add another action to this action. Though it is possible and when these two keys are clamped. No difference. Tried from some other experience like this:

LRESULT CALLBACK UpdateMessagesWindow(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
  {
    case WM_KEYDOWN:
      CheckPressKey(wParam);
      break;
  }
}

void CheckPressKey(keyid)
{
if(keyid & 0x41) // A 
{

}

if(keyid & 0x57) // W
{

}
}

But the code in the check is executed by pressing absolutely any key, and not by "A" and "W". How to be?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jcmvbkbc, 2017-09-26
@s2sk1337

the code in the check is executed by pressing absolutely any key, and not by "A" and "W". How to be?

Just read the description of WM_KEYDOWN . wParam is not a bitmask, it's a key code, you don't have to write if(keyid & 0x41), you have to write if(keyid == 0x41).

A
Andrei Smirnov, 2017-09-25
@pinebit

There are also two events: WM_KEYDOWN and WM_KEYUP.
You need to store two flags: is_A_pressed, is_B_pressed.

void handleKeyDown(keyid)
{
if(keyid & 0x41) // A 
{
  is_A_pressed = true;
}

if(keyid & 0x57) // W
{
 is_W_pressed = true;
}

if (is_A_pressed && is_W_pressed) {
   // обе клавиши нажаты
}
}

the same code in the WM_KEYUP handler, only we reset the flags there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question