Sign in to follow this  
Followers 0
Obs

Handling Keyboard/ Mouse events

3 posts in this topic

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!

[code]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);
}[/code]
0

Share this post


Link to post
Share on other sites
You're missing a final break statement in case WM_KEYDOWN. It is falling through into the WM_LBUTTONDOWN case.
1

Share this post


Link to post
Share on other sites
[quote name='rip-off' timestamp='1307742611' post='4821876']
You're missing a final break statement in case WM_KEYDOWN. It is falling through into the WM_LBUTTONDOWN case.
[/quote]

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0