Handling Keyboard/ Mouse events

Started by
2 comments, last by DividedByZero 12 years, 10 months ago
I'm wondering if anyone can educate/ shed some light on something I have come across. I'm building a DirectX application in C++ and handling keyboard mouse events in my winProc function. My code works, but only when I test for (wParam == MK_LBUTTON) and (wParam == MK_RBUTTON) respectively after picking up the messages WM_LBUTTONDOWN and WM_RBUTTONDOWN and handling them in my switch statement. Without testing for those conditions the left and right-click actions still execute correctly, it is the escape key that goes wrong. Instead of closing the application, it throws out the behaviour tied to a left-click.

I have a feeling the answer to this will teach me something, so I'd be grateful if someone has an answer. Why does the program let the WM_LBUTTONDOWN switch case handle a WM_KEYDOWN message and why is it being handled in that switch statement when there is a perfectly good switch statement prior to it that should handle it. The close button is still handled, so I know it is entering the first switch statement.

I suspect I'm just not understanding the whle concept, so I did dig out an DirectX book to see how they did things, and their code was almost identical to mine - minus the test conditions! Been reading topics on here and MSDN all day, and I have no idea what is going on.

Confused!

LRESULT CALLBACK winProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
// check message on queue (if any)
switch (message)
{
case WM_KEYDOWN: // if keystroke registered
switch(wParam) // switch key
{
case VK_ESCAPE: // if escape key
PostQuitMessage(0); // post WM_QUIT message to threads message queue
break;
case WM_DESTROY: // if close button clicked
PostQuitMessage(0); // post WM_QUIT message to threads message queue
break;
default:
break;
}
case WM_LBUTTONDOWN: // if left-click registered
if (wParam == MK_LBUTTON) // and wParam is left-click
{
// test code: increases window by 250px in every direction
width+=500;
height+=500;
SetWindowPos(wndHandle, HWND_TOP, (GetSystemMetrics(16)-width)/2,
(GetSystemMetrics(17)-height)/2, width, height, SWP_SHOWWINDOW);
}
break;
case WM_RBUTTONDOWN: // if right-click registered
if (wParam == MK_RBUTTON) // and wPparam is right-click
{
// test code: decreases window by 250px in every direction
width-=500;
height-=500;
SetWindowPos(wndHandle, HWND_TOP, (GetSystemMetrics(16)-width)/2,
(GetSystemMetrics(17)-height)/2, width, height, SWP_SHOWWINDOW);
}
break;
default:
break;
}

// return message to default window procedure for handling
return DefWindowProc(hWnd, message, wParam, lParam);
}
Advertisement
You're missing a final break statement in case WM_KEYDOWN. It is falling through into the WM_LBUTTONDOWN case.

You're missing a final break statement in case WM_KEYDOWN. It is falling through into the WM_LBUTTONDOWN case.


ARGH! A day wasted! I obsessively checked my break statements throughout the day and just couldn't see it.

Thank you so much - an extra pair of eyes really do make the difference.
@rip-off - Nice catch. I was looking for the same thing in his code. :cool:

This topic is closed to new replies.

Advertisement