sysop
sysop

Reputation: 810

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.

Upvotes: 5

Views: 7936

Answers (4)

phoff
phoff

Reputation: 183

In my current project we're setting 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 (eg, numpad keys), which we need to do in this project. We then define "normal" key events in WM_KEYUP.

I'm not claiming this is necessarily the best solution for your particular problem, but it is another option, and is an excellent fit for certain sets of needs.

Upvotes: 0

Lefteris
Lefteris

Reputation: 3256

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.

Upvotes: 4

It's been a while, but your wndproc should have several parameters, one of them being the wParam. The wParam will have the virtual key codes in question. depending on how in depth you want to get you may want to have an inner switch, like so:

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

Upvotes: 0

Greg Hewgill
Greg Hewgill

Reputation: 992707

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.

Upvotes: 1

Related Questions