Processing Shift Keys

When CONNECT receives a WM_MOUSEMOVE message, it performs a bitwise AND operation on the value of wParam and MK_LBUTTON to determine if the left button is depressed. You can also use wParam to determine the state of the Shift keys. For instance, if processing must be dependent on the status of the Shift and Ctrl keys, you might use logic that looks like this:

if (MK_SHIFT & wParam)

if (MK_CONTROL & wParam)

{

[Shift and Ctrl keys are down]

}

else

{

[Shift key is down]

}

else if (MK_CONTROL & wParam)

{

[Ctrl key is down]

}

else

{

[neither Shift nor Ctrl key is down]

}

The Windows function GetKeyState (described in Chapter 3) can also return the state of the mouse buttons or shift keys using the virtual-key codes VK_LBUTTON, VK_RBUTTON, VK_MBUTTON, VK_SHIFT, and VK_CONTROL. The button or key is down if the value returned from GetKeyState is negative. Because GetKeyState returns mouse or key states as of the message currently being processed, the status information is properly synchronized with the messages. But just as you cannot use GetKeyState for a key that has yet to be pressed, so you cannot use it for a mouse button that has yet to be pressed. Don't do this:

while (GetKeyState (VK_LBUTTON) >= 0) ; // WRONG !!!

The GetKeyState function will report that the left button is depressed only if the button is already depressed when you process the message during which you call GetKeyState.