Sign in to follow this  
CulDeVu

Getting Mouse Events on Desktop in C++

Recommended Posts

Hi,

I want to be able to detect mouse clicks and releases (primarily releases) anywhere on the desktop. I have the mouse position with GetCursorPos(LPOINT) and track mouse movement like that, but I don't know how to get any other events off of my program. Is there a function that allows me to do that?

Share this post


Link to post
Share on other sites
[quote name='ryan20fun' timestamp='1306215284' post='4814894']
have you tried useing DirectX(it works on the driver level) and it seams to report updates and such that are not in your window
[/quote]

Well I was wanting to not get DirectX involved, just using the Win32. Sorry, but DirectX just seems like overkill (what with COMs and stuff) and I'm still learning the basics of it. Is there no Win32 function that detects mouse clicks?

Share this post


Link to post
Share on other sites
[quote name='lonewolff' timestamp='1306274106' post='4815325']
In that case you just simjply, process WM_MOUSEDOWN & WM_MOUSEUP messages.
[/quote]

Those only get the events inside your current window. I need a function sort of like GetCursorPosition() that will get me the messages anywhere on the desktop. I've looked online and have found nothing in C++.

Share this post


Link to post
Share on other sites
[code]
char keys[255]


for(int i = 0; i < 256; i++)
{
keys[i] = (char) (GetAsyncKeyState(i) >> 8);
}
[/code]

if keys[1] = 1 then the left button is down
if leys[2] = 1 then the right button is down

I'm using that to get the keyboard at the moment, haven't started on mouse stuff for my program but that might help if you just need to find out if the mouse buttons are down or not.
I'm about to play around with the mouse so I'll paste the code if I get it done soon :)

Sorry if keys[1] and keys[2] != 0 then they're down, I think the actual value is -128 or something, I just check != 0 to keep it simple Edited by malborojones

Share this post


Link to post
Share on other sites
Just knocked this up for you

[code]

struct MouseStates
{

int m_leftState;
int m_rightState;
int m_lastLeft;
int m_lastRight;

char m_mouse[2];

void UpdateMouse()
{
//set old states
m_lastLeft = m_leftState;
m_lastRight = m_rightState;
//Find out if the button is down
m_mouse[0] = (char) (GetAsyncKeyState(1) >> 8);
m_mouse[1] = (char) (GetAsyncKeyState(2) >> 8);

if (m_mouse[0] != 0) m_leftState = 1;
else m_leftState = 0;

if (m_mouse[1] != 0) m_rightState = 1;
else m_rightState = 0;


}

int GetButtonState(int button)
{
switch(button)
{
case 1:

//Check if it's up
if ( m_leftState == 0 && m_leftState == m_lastLeft ) return 0;
//Check if it's just gone down
if ( m_leftState == 1 && m_leftState != m_lastLeft ) return 1;
//Check if it's been released
if ( m_leftState == 0 && m_leftState != m_lastLeft ) return 2;
//Check if it's held
if ( m_leftState == 1 && m_leftState == m_lastLeft ) return 3;

break;
case 2:

//Check if it's up
if ( m_rightState == 0 && m_rightState == m_lastRight ) return 0;
//Check if it's just gone down
if ( m_rightState == 1 && m_rightState != m_lastRight ) return 1;
//Check if it's been released
if ( m_rightState == 0 && m_rightState != m_lastRight ) return 2;
//Check if it's held
if ( m_rightState == 1 && m_rightState == m_lastRight ) return 3;
break;

default:
return -1;
}
}

} mMouse;
[/code]


Just call Update every loop and then find out what state the buttons are in like this:

[code]

mMouse.UpdateMouse();

//Check if mouse is up
if(mMouse.GetButtonState(1) == 0)
SetWindowText(mHwnd, "Left Button Up");

//Check if mouse has just been clicked
if(mMouse.GetButtonState(1) == 1)
SetWindowText(mHwnd, "Left Button Clicked");

//Check if mouse has just been released
if(mMouse.GetButtonState(1) == 2)
SetWindowText(mHwnd, "Left Button Released");

//Check if mouse is held
if(mMouse.GetButtonState(1) == 3)
SetWindowText(mHwnd, "Left Button Held");
[/code]


That works perfectly for me whether I click in the window or not.
Good luck, glad I could help [img]http://public.gamedev.net/public/style_emoticons/default/smile.gif[/img]

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