Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 necessary parameters from here and here.

But I don't know how to combine them properly.

like image 965
sysop Avatar asked Feb 09 '12 04:02

sysop


2 Answers

Assuming that 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 is a bit trickier you have to use a different way

Why like that? You will notice from WM_LBUTTONDOWN page that for each signal sent you have parameters given. One of them is the wparam. It can have different values depending on whether some special keys are pressed or not

And since the wparam of the WM_LBUTTONDOWN signal does not contain information about the alt button you would have to utilize the GetKeyState function which returns a high order bit value of 1 if the key is down and anything else if it's not.

like image 181
Lefteris Avatar answered Sep 23 '22 16:09

Lefteris


Use the GetKeyState function to get the state of the modifier keys at the time the current message was generated. So:

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

and so on. Note that you will want to check for the multi-key combinations before a single shift key, otherwise the single shift test will succeed even if some other modifier key is down.

like image 34
Greg Hewgill Avatar answered Sep 23 '22 16:09

Greg Hewgill