Jump to content
  • Advertisement
Sign in to follow this  
Malazar

Windows API mouse input questions

This topic is 4080 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to use WM_LBUTTONDOWN to detect when i'm pressing the (left) mouse button, however i can't make heads nor tails of the msdn/visual studio help entry for these notifications, it doesn't seem to tell me how to call it. right now i went for the (to my mind, and lacking a better idea) logical call: if (WM_LBUTTONDOWN) { //do stuff } of course, this doesn't do a damn thing (infact my program doesn't draw the contents of the window anymore) though it does compile. so my question is this: how do i call this propperly, so that my program detects when the user presses a mouse button? thanks in advance, guys. Mal'

Share this post


Link to post
Share on other sites
Advertisement
that code is logically OK i believe, but it won't be too useful :) . are you using an LRESULT CALLBACK function to check for windows messages?

if so, then it'd be a good idea to have a switch block with possible messages (if you're building on example code you should hopefully have something like this...).

eg..

LRESULT CALLBACK WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
switch (uMsg) // Check For Windows Messages
{

...
case WM_LBUTTONDOWN:
MessageBox(NULL, NULL, NULL, MB_OK);
break;
}//end of switch
}//end of function


note that this creates a message box every time you left click, including when you click the 'ok' to close the message box, so maybe a less annoying indication that the code works would be a good idea ;)


EDIT:I should put in an explanation of whats going on.

The message you get isn't a unique variable, but more like a variable being set. i think you see it as being:

bool WM_LBUTTONDOWN;//left mouse button pressed
bool WM_RBUTTONDOWN;//right mouse button pressed
bool WM_MOUSEMOVE; //mouse mooved
...etc for each key

whereas it's actually more like

int someMessage; //value of some message being sent to the window

where somethingPressed is set and passed to WndProc. Therefore calling
if(somethingPressed) will allways evaluate to true.


A basic approach to this is to throw a switch block into the WndProc function for each case you want to catch (like i posted earlier). if the function starts to get too big, you can allways pass the data on to an input class.

[Edited by - Winegums on April 18, 2007 9:02:29 PM]

Share this post


Link to post
Share on other sites
ah ha, thanks, you're right about how i was reading the messages.

one thing though, when i put your callback function into my code (replacing the message box with what i was trying to get the mouse button to do in the first place) i get the error message that the WndProc must return a value - however adding a return 0 instruction doesn't seem to do the trick (as nothing happens when i hit the mouse button - even with your message box).

any thoughts?

Share this post


Link to post
Share on other sites
one last question, i'm not sure how to invoke this function, does it just run in the background, or do you have to call it some way?

Share this post


Link to post
Share on other sites
right, that's this function, right?:

LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY:
Cleanup();
PostQuitMessage( 0 );
return 0;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}

to which i should add:
LRESULT DispatchMessage(const MSG uMsg);

right? and if so, does it need to be inside the switch, or outside?

Share this post


Link to post
Share on other sites
Hey.

I'll point you to this for a quick tutorial on the Windows API and a review on the message architecture it uses.

EDIT: Fixed link.

Share this post


Link to post
Share on other sites
thanks, i'll read through that now.
it's odd, i've not managed to find any win API tutorials that cover mouse input, they only seem to deal with keyboards (which are a hell of alot easier to set up it seems).

thanks again to everyone who's helped :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!