Search code examples
c++winapikeyboardmouse

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.


Solution

  • 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.