views:

427

answers:

6

How do I get the coordinates of my mouse when left/right mouse button has been pressed?

I am using a low level mouse hook and am able to get the current position of my cursor, but I would like to be able to retrieve the position when any mouse button has been pressed.

How can I do this?

A: 

call GetMessagePos() on WM_LBUTTONDOWN to get what you want. But I doubt that it will work inside a low-level mousehook. It's meant to be used in your message pump or window proc.

"The GetMessagePos function retrieves the cursor position for the last message retrieved by the GetMessage function."

Are you sure you need a hook?

John Knoeller
Yes, I need a hook. Thank you! Will look at the GetMessagePos().
Zolomon
If you need a low level hook, then you shouldn't be writing managed code. It's important that the mouse be responsive - a sluggish mouse makes everything more difficult.
John Knoeller
Well, I don't know C++ so I can't write a DLL for it on my own, and it's only for personal use. Writing the coordinates of mousebutton clicks to a file isn't too performance demanding I hope?
Zolomon
for personal use? do whatever you want. I think you will notice the sluggishness - if it bugs you too much you can try your hand a C++. A basic dll really isn't that hard - especially if you use VisualStudio.
John Knoeller
The sluggishness won't be from the clicks, it will be because every mouse event (mostly mouse moves) will have to go from unmanaged code where the kernel lives, to managed code to call your hook function.
John Knoeller
Aha, thanks! I'll give it a try later if I can get GetMessagePos() to work. How can I use GetMessagePos() on WM_LBUTTONDOWN?
Zolomon
A: 

In your MouseHook method:

public static int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)
{
    //Marshall the data from the callback.
    MouseHookStruct MyMouseHookStruct = 
         (MouseHookStruct) Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));

    if (nCode >= 0)
    {
        int xcoord = MyMouseHookStruct.pt.x;
        int ycoord = MyMouseHookStruct.pt.y;
    }

    return CallNextHookEx(hHook, nCode, wParam, lParam); 
}

From here.

Mitch Wheat
Yeah, this is the hook I'm using. Though, your answer won't give me the coordinates when any mouse button has been pressed. It'll just give me the current coordinates of the mouse, and you forgot the return statement. Thanks though!
Zolomon
So, you can pretty much take it from here then?
Hans Passant
No, I still don't manage to retrieve the coordinates with GetMessagePos() from user32.dll, don't know how to use it with the procedure. Help would be appreciated. :)
Zolomon
A: 

The wParam argument of your MouseHook procedure will contain the message identifier WM_LBUTTONDOWN, WM_LBUTTONUP, WM_RBUTTONDOWN, WM_RBUTTONUP, etc.. from this you can determine what the button state is at the current coordinates.

+2  A: 

Why don't you just capture the MouseDown Event, and from the MouseEventArgs, obtain the position of the click by using MouseEventArgs.Location?

Ngu Soon Hui
Because this would only work when the application has focus?
Zolomon
The context of OP wasn't clear. If it is within the context of a Forms app, then Ngu's solution is by far the simplest. Outside of that context it won't work at all :)
Jeff Kotula
A: 

http://www.codeproject.com/KB/vb-interop/MouseHunter.aspx - I found this little charming piece of information. Sadly Visual Studio 2008 won't accept the dll that's been precompiled, and I can't get Visual Basic 6 to install on my machine to try to recompile it.

Zolomon
A: 

http://www.codeproject.com/KB/system/globalsystemhook.aspx - this solved my problem. Used the DLL's from the demo project and managed to get the coordinates.

Zolomon