Thread: capturing mouse events

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    39

    Question capturing mouse events

    Hi all,

    I would like to know if there is any method of capturing mouse events directly from the OS? Normally we try to tap into a windows procedure call to learn what the mouse is doing but I want to do this on a global scale regardless of which window is active.
    Basically I would like to hide the cursor whenever the middle button is clicked(any where on the screen) and appear otherwise

    Regards

  2. #2
    Registered User
    Join Date
    May 2005
    Posts
    39

    Question

    Quote Originally Posted by The Brain
    I would handle the WM_MBUTTONDOWN messages.. and hide the cursor. Then when a WM_MBUTTONUP message is issued, show the cursor:

    Code:
    switch(message)
    {
         case WM_MBUTTONDOWN:  ShowCursor(FALSE);
                               break;
    
         case WM_MBUTTONUP:    ShowCursor(TRUE);
                               break;
    }
    Thanks for reply,but...

    Where will I get the 'message' value? I am building a standalone application which will continously monitor for mouse activity...how can i force the OS to send it messages related to mouse events?

  3. #3
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Sorry.. I just deleted that post because it was not a global solution.

    I am not sure how to share input focus w/ another application
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    You can use a windows hook. Here is a quick sample using WH_MOUSE_LL:
    Code:
    #define _WIN32_WINNT 0x0500
    #include <windows.h>
    #include <stdio.h>
    
    HHOOK g_hMouseHook;
    
    LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
    {
    	if (nCode >= 0)
    	{
    		PMSLLHOOKSTRUCT pmll = (PMSLLHOOKSTRUCT) lParam;
    
    		printf("msg: %lu, x:%ld, y:%ld\n", wParam, pmll->pt.x, pmll->pt.y);
    
    		switch (wParam)
    		{
    			case WM_MBUTTONDOWN:
    				ShowCursor(FALSE);
    				break;
    
    			case WM_MBUTTONUP:
    				ShowCursor(TRUE);
    				break;
    		}
    	}
    
    	return CallNextHookEx(g_hMouseHook, nCode, wParam, lParam);
    }
    
    
    int main(void)
    {
    	MSG msg;
    
    	g_hMouseHook = SetWindowsHookEx( WH_MOUSE_LL, LowLevelKeyboardProc, GetModuleHandle(NULL), 0 );
    
    	if (!g_hMouseHook) printf("err: %d\n", GetLastError());
    
    	while ( GetMessage(&msg, NULL, 0, 0) )
    	{
    	        TranslateMessage(&msg);
    	        DispatchMessage(&msg);
    	}
    
    	UnhookWindowsHookEx(g_hMouseHook);
    	return (int) msg.wParam;
    }
    Using WH_MOUSE will be far more efficient (WH_MOUSE_LL may noticeably slow down your computer) but the hook procedure must be placed in a DLL. This allows the hook procedure to be loaded in every process and run in-process. A WH_MOUSE_LL procedure on the other hand must switch back to the original process each time to run out-of-process. This is quite a slow operation. Search on the board or the web for "SetWindowsHookEx" for examples for WH_MOUSE.

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    39

    Smile

    Hi anonytmouse,

    That looks like a perfectly good code! But I'm facing one slight problem. The 'ShowCursor(FALSE)' is not able to hide the cursor. I even checked the return value and it does become (-1). I even tried another function 'SetCursor(NULL)' in its place, but even that doesn't help.
    Does it have anything to do with my OS (WinXP professional)?

    so near...yet so far...

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Try this:
    1. When the button is pressed down save the x and y position.
    2. Use the SetCursorPos function to postion the cursor off screen.
    3. When the button is released restore the mouse postion.

  7. #7
    Registered User
    Join Date
    May 2005
    Posts
    39

    Cool

    Hi anonytmouse,

    That looks like a pretty nasty trick But the problems is that it actually moves the 'cursor location' to unwanted territories. I would like to use my cursor when its hidden and so I gues this won't be appropriate. Basically I would also like to hide the cursor while I am drawing on a surface using a stylus (that means hidding the cursor on a left click,drag etc. and revealing the cursor when there is only a mouse move and no drag operation)

    Could I use some other methods like maybe changing the cursor image to a point (small enough to be not visible) or maybe change its color to one that matches the background?

    I still don't get it...why the ShowCursor stuff doesn't work

  8. #8
    Registered User
    Join Date
    May 2005
    Posts
    39

    Lightbulb

    Hi anonytmouse,

    Just thought I would update you on my status.
    After trying all methods and means to hide the cursor, I finally decided that if I can't hide it...I would instead change it.
    I used the SetSystemCursor() to change the default arrow cursor to a 'blank' cursor. (although doing this will overright the arrow cursor data and the only way to bring it back is to reload the lost cursor or restart the PC)
    This did solve the problem, but it resulted in another. I am unable to take a copy of the original cursor and restore it as it is. CopyCursor() only captures the handle (and not the data)
    I have decided to start a new thread on this topic and hope somebody helps me out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mouse Events
    By tezcatlipooca in forum Windows Programming
    Replies: 2
    Last Post: 01-24-2008, 02:33 PM
  2. Watch for mouse events
    By belhifet in forum Linux Programming
    Replies: 2
    Last Post: 04-10-2007, 11:12 AM
  3. Mouse Events
    By disruptor108 in forum C# Programming
    Replies: 0
    Last Post: 01-23-2007, 11:40 PM
  4. API for mouse events?
    By jsimpson in forum C Programming
    Replies: 0
    Last Post: 03-08-2006, 11:38 PM
  5. Mouse Events
    By Jubba in forum Windows Programming
    Replies: 1
    Last Post: 10-30-2003, 12:03 PM