tags:

views:

61

answers:

2

Currently I am detecting the x and y position of a mouse click, storing it in a Point and displaying it through the message box.

I want to be able to read if another keyboard key is being held down such as the Shift or Control button.

Looking on MSDN I found the following information:

wParam Indicates whether various virtual keys are down. This parameter can be one or more of the following values.

MK_CONTROL The CTRL key is down.

MK_MBUTTON The middle mouse button is down.

MK_RBUTTON The right mouse button is down.

MK_SHIFT The SHIFT key is down.

MK_XBUTTON1 Windows 2000/XP: The first X button is down.

MK_XBUTTON2 Windows 2000/XP: The second X button is down.

The problem I am having is I'm unsure as to how to store the results from wParam for each parameter and use them like I have to display them through the message box.

Here is my progress so far:

LRESULT CALLBACK WindowFunc(HWND hMainWindow, UINT message,
    WPARAM wParam, LPARAM lParam)
{
 POINTS mouseXY;
 WCHAR buffer[256];

    // Act on current message
    switch(message)    
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
 case WM_LBUTTONUP:
  // Get mouse x, y
  mouseXY = MAKEPOINTS(lParam);

  // Output the co-ordinates
  swprintf(buffer, 255, L"x = %d, y = %d", mouseXY.x, mouseXY.y);
  MessageBox(0, buffer, L"Mouse Position", MB_OK);
  break;
    default:
        return DefWindowProc(hMainWindow, message, wParam, lParam);
    }
    return 0;
}

Thanks for the help

+1  A: 

You can use GetAsyncKeyState to find out the state of the most of the buttons:

SHORT lshift = GetAsyncKeyState(VK_LSHIFT);
SHORT rshift = GetAsyncKeyState(VK_RSHIFT);
// etc...

Here is a description of the difference between GetKeyState and GetAsyncKeyState.

You can also use GetKeyboardState:

BYTE keyboardState[256];
GetKeyboardState(keyboardState);
Ryan
Thank you for the explanation.
Jamie Keeling
+1  A: 

The different virtual keys are ORed together in wParam. To check for single values, you have to AND them out (think basic bit operations).

Example:

swprintf(buffer, 255, L"x = %d, y = %d, Shift = %s, Ctrl = %s",
         mouseXY.x, mouseXY.y,
         wParam & MK_SHIFT ? L"yes" : L"no",
         wParam & MK_CONTROL ? L"yes" : L"no");
gclj5
Works excellently, had to add an L just before each "yes" and "no" to get the correct output.Thanks.
Jamie Keeling
You're welcome. And you're right, I forgot the Ls. I added them to my response.
gclj5