Thread: Mouse Hook Question (Can't determine whether or not button is XBUTTON1 or XBUTTON2)

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155

    Mouse Hook Question (Can't determine whether or not button is XBUTTON1 or XBUTTON2)

    Hey.

    I've got a low level mouse hook that is keeping track of button clicks (lbutton,rbutton,mbutton,xbutton). However, when xbutton is pressed, I cannot figure out how to determine whether or not it was the button1 or button2. It doesn't appear to think it's either XBUTTON1 or XBUTTON2.

    Here's the hook (there's some code in there that doesn't matter, for example: the maps).
    Code:
    LRESULT CALLBACK CHook::LLMouseProc(int nCode, WPARAM wParam, LPARAM lParam)
    {
    	if ( nCode == HC_ACTION )
    	{
    		if ( wParam == WM_LBUTTONDOWN || wParam == WM_RBUTTONDOWN || wParam == WM_MBUTTONDOWN || wParam == WM_XBUTTONDOWN ) //Mouse key down...
    		{
    			keytype kt;
    
    			kt.type = TYPE_MOUSE;
    			kt.mouseCode = wParam;
    
    			switch ( wParam )
    			{
    			case WM_LBUTTONDOWN:
    				kt.name = _T("Left Mouse");
    				break;
    			case WM_RBUTTONDOWN:
    				kt.name = _T("Right Mouse");
    				break;
    			case WM_MBUTTONDOWN:
    				kt.name = _T("Middle Mouse");
    				break;
    			case WM_XBUTTONDOWN:
    				
    				int whichbtn = GET_KEYSTATE_WPARAM ( wParam );
    
    				if ( whichbtn == XBUTTON1 )//ugh! always prints out Mouse 5
    					kt.name = _T("Mouse 4");
    				else
    					kt.name = _T("Mouse 5");
    
    				break;
    			}
    			
    			CHook::activeKeys.insert ( make_pair ( wParam, kt ) );
    
    			if ( CHook::KeyIsDropped ( wParam ) ) return 1; //If we need to filter this key, do so
    		}
    		else if ( wParam == WM_LBUTTONUP || wParam == WM_RBUTTONUP || wParam == WM_MBUTTONUP || wParam == WM_XBUTTONUP )
    		{
    			CHook::keyReleased = true; //A key has been released - set the variable to it
    
    			//Remove the key from the map (it has been released now...)
    			map<long, keytype>::iterator i = CHook::activeKeys.find ( wParam );
    			if ( i != CHook::activeKeys.end() )
    			{
    				CHook::activeKeys.erase ( i );
    			}
    		}
    	}
    
    	return CallNextHookEx ( CHook::hMouseHook, nCode, wParam, lParam );
    }
    Any help concerning this topic would be greatly appreciated.


    Thanks,

    Matt N
    ~guitarist809~

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    I think you should be checking the return of GET_KEYSTATE_WPARAM with MK_XBUTTON1.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    Quote Originally Posted by DaveH View Post
    I think you should be checking the return of GET_KEYSTATE_WPARAM with MK_XBUTTON1.
    Thanks for the advice.

    Unfortunatly, it still only goes with "Mouse 5" for both of the side mouse buttons.

    I replaced that code with

    Code:
    if ( GET_KEYSTATE_WPARAM ( wParam ) == MK_XBUTTON1 )
    	kt.name = _T("Mouse 4");
    else
    	kt.name = _T("Mouse 5");
    The function GET_KEYSTATE_WPARAM() is always returning the number 523, regardless of which mouse button I am pressing (mouse 4 or 5).

    Is this too much to much to ask of Windows in a low-level hook? I'm thinking it might be because it hasn't gone through any processing yet in the other input threads.

    If there is a way around this please let me know.

    Thanks,

    Matt N
    Last edited by guitarist809; 12-04-2008 at 12:20 PM.
    ~guitarist809~

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    137
    See on Adv. Win32 api ng news://194.177.96.26/comp.os.ms-wind...ogrammer.win32
    where the official method has been given dozens of times...

  5. #5
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Have you tried using the GET_XBUTTON_WPARAM macro?

  6. #6
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    Quote Originally Posted by Alex31 View Post
    See on Adv. Win32 api ng news://194.177.96.26/comp.os.ms-wind...ogrammer.win32
    where the official method has been given dozens of times...
    I've tried going here. There was nothing useful that helped me.


    Quote Originally Posted by BobS0327
    Have you tried using the GET_XBUTTON_WPARAM macro?
    I have and unfortunately, I have had no success =\
    ~guitarist809~

  7. #7
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    I'm pulling at straws here since I don't have a mouse with that capability. But anyway, the WM_MOUSEHWHEEL message may be worth checking out.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    To check the get_param return values, you should be &&, not checking ==. At least poking through my copy of winuser.h, GET_KEYSTATE_WPARAM being 523 corresponds to Ctrl + left button + right button (and neither of the x buttons).

  9. #9
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    Quote Originally Posted by tabstop View Post
    To check the get_param return values, you should be &&, not checking ==. At least poking through my copy of winuser.h, GET_KEYSTATE_WPARAM being 523 corresponds to Ctrl + left button + right button (and neither of the x buttons).
    Hey

    I tried the following code, but it still didn't work.

    Code:
    if ( GET_XBUTTON_WPARAM ( wParam ) && XBUTTON2  )
    {
    	OutputDebugString ( _T("Button 2\n") );
    }
    else
    {
    	OutputDebugString ( _T("Button 1\n") );
    }
    I've used a bitwise operator (&), the && sign, and the == sign. None appear to work. I've also used these in combonation with the GET_XBUTTON_WPARAM, GET_KEYSTATE_WPARAM, HIWORD, and LOWORD macros to see if it would work.

    There has gotta be something I'm doing wrong.

    Thanks for the help so far guys, I appreciate it.

    Thanks,

    Matt N
    ~guitarist809~

  10. #10
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    Hello again.

    I was experimenting with every possible thing I could think of and I finally got a result.

    Code:
    PMSLLHOOKSTRUCT p = (PMSLLHOOKSTRUCT) lParam;
    
    		if ( GET_XBUTTON_WPARAM ( p->mouseData ) == XBUTTON1 )
    		{
    			OutputDebugString ( L"Mouse button 1 pressed.\n" );
    		}
    		else if ( GET_XBUTTON_WPARAM ( p->mouseData ) == XBUTTON2 )
    		{
    			OutputDebugString ( L"Mouse button 2 pressed.\n" );
    		}

    Apparently, the wParam variable has no LOWORD() value, so I found it in the mouseData variable in the mouse hook struct.


    Thanks for your time,

    Guitarist809
    ~guitarist809~

  11. #11
    Registered User
    Join Date
    Apr 2007
    Posts
    137
    Quote Originally Posted by guitarist809 View Post
    I've tried going here. There was nothing useful that helped me.
    I have and unfortunately, I have had no success =\
    Thern learn to search or to post a question, as it has been answered dozens of times for > 15 years...

  12. #12
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    Quote Originally Posted by Alex31 View Post
    Thern learn to search or to post a question, as it has been answered dozens of times for > 15 years...
    I did. It only showed the results from the last month. (288 total I think).
    ~guitarist809~

  13. #13
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    Quote Originally Posted by tabstop View Post
    To check the get_param return values, you should be &&, not checking ==. At least poking through my copy of winuser.h, GET_KEYSTATE_WPARAM being 523 corresponds to Ctrl + left button + right button (and neither of the x buttons).
    perhaps this was a typo, try using & (bitwise) comparison.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. hook question
    By milkk in forum Windows Programming
    Replies: 3
    Last Post: 07-13-2007, 11:58 AM
  2. button question
    By Rare177 in forum C# Programming
    Replies: 2
    Last Post: 01-05-2007, 10:59 AM
  3. Code for holding down right mouse button
    By Bill83 in forum Windows Programming
    Replies: 2
    Last Post: 03-04-2006, 07:28 PM
  4. opengl question. mouse location vs gluperspective
    By revelation437 in forum Game Programming
    Replies: 1
    Last Post: 10-19-2004, 07:18 PM
  5. Mouse question
    By TonyP in forum Windows Programming
    Replies: 3
    Last Post: 05-20-2003, 07:43 PM