Jump to content

  • Log In with Google      Sign In   
  • Create Account

Handling Keyboard/ Mouse events


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Obs   Members   -  Reputation: 101

Like
0Likes
Like

Posted 10 June 2011 - 03:39 PM

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);
}


Sponsor:

#2 rip-off   Moderators   -  Reputation: 8764

Like
1Likes
Like

Posted 10 June 2011 - 03:50 PM

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

#3 Obs   Members   -  Reputation: 101

Like
0Likes
Like

Posted 10 June 2011 - 03:57 PM

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.

#4 DarkRonin   Members   -  Reputation: 616

Like
0Likes
Like

Posted 10 June 2011 - 11:44 PM

@rip-off - Nice catch. I was looking for the same thing in his code. :cool:




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS