Win32 mouse and keyboard combination

I need to combine mouse and keyboard events in Win32 like Click + Shift or Click + Alt + Shift .

For example (pseudo code):

 case WM_LBUTTONDOWN: if (Shift) //click+Shift if (Shift && Ctrl) //click+Shift+Ctrl if (Shift && Alt) //click+Shift+Alt break; 

I know all the necessary parameters from here and here .

But I do not know how to combine them correctly.

+4
source share
4 answers

Assuming this is inside your winproc:

 if(wParam & MK_SHIFT) { if (wParam & MK_CONTROL && wParam & MK_SHIFT) { //click+Shift+Ctrl } else if(wParam & MK_SHIFT && HIBYTE(GetKeyState(VK_MENU)) & 0x80) { //alt+shift } else { //just shift } } 

Shift and click and alt are a bit more complicated, you have to use a different way

Why is that? You will see on the page WM_LBUTTONDOWN that for each sent signal you have the specified parameters. One of them is wparam. It can have different meanings depending on whether any special keys are pressed or not.

And since the wparam of the WM_LBUTTONDOWN signal does not contain information about the alt button, you will need to use the GetKeyState function, which returns a high bit of order 1 if the key is omitted, and something else if it is not.

+2
source

Use the GetKeyState function to get the state of the modifier keys during the creation of the current message. So:

 if (GetKeyState(VK_SHIFT) < 0 && GetKeyState(VK_CONTROL) < 0) { // click+shift+ctrl } else if (GetKeyState(VK_SHIFT) < 0) { // click+shift } 

etc. Please note that you will need to check the combination with several keys before one shift key, otherwise the one-time test will succeed even if some other modifier key does not work.

+1
source

It has been a while, but your wndproc should have several parameters, one of which is wParam. WParam will display the virtual key codes. depending on how you want to get the depth, you can have an internal switch, for example:

 switch (wParam) { case MK_CONTROL: { // handle mouse and ctrl key down break; } } 
0
source

In my current project, we set the flags in the WM_KEYDOWN section of our wndproc:

 case WM_KEYDOWN: { switch(wParam) { case VK_CONTROL: { isHoldingCtrl = true; } break; case VK_SHIFT: { isHoldingShift = true; } break; case VK_NUMPAD1: } numPad = 1; } break; } } break; 

This allows us to use any key as a modifier (for example, the numpad key), which we need to do in this project. Then we define the “regular” key events in WM_KEYUP.

I am not saying that this is necessarily the best solution for your specific problem, but it is another option and is great for certain sets of needs.

0
source

Source: https://habr.com/ru/post/1395542/


All Articles