[.net] Mouse delta

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

Recommended Posts

Hi. I have just one quick question about mouse input. Is there a simple way to get the delta the mouse has moved? I tried using MouseMove event of control but it only returns absolute position of mouse cursor. I could use the difference from last position but it doesn't work when mouse is on border. I need to get delta even if for example mouse is on left side (x=0) and I move it a bit more left. Absolute position should stay the same but I need to get the amount the mouse has really moved. Any help would be useful. -Mirko

Share on other sites
I'm not sure if it'll help, but you can translate from absolute to client coordinates (with the top-left of your window being 0,0) by calling this from inside your Form:
Point clientCoords = this.PointToClient(Cursor.Position);

Share on other sites
I think you should look at the Control.Capture property, but then, it depends on what you want to achieve exactly.

(Capturing the mouse will allow you to receive MouseMove events, even if the mouse is moving outside your control).

Share on other sites
Thank you but I know about PointToClient and Capture and they don't help me much with my problem. I have no problem capturing mouse movements outside my control. I have a problem getting mouse movements that would end up outside the screen. Is there some kind of function to PInvoke to get this info. I'm looking for something that doesn't need some external library.

Share on other sites
DirectInput was giving you mouse delta (at least it did when I was using it :)

Without DirectX or any other external library I don't know if there's a good way of doing it. But there's always the ugly way :)
If you are doing an FPS, you could hide the cursor, place it at the center of the form and hold it there. That is, every loop you check the mouse position. If it's at the center of the form, delta is 0. Otherwise, calculate the delta and return the mouse to the center of the form.

I didn't try that, but it should work.

Share on other sites
In Windows the cursor is not allowed to leave the screen, that's why you can't get any delta when the user tries to.

If all you want is get the real mouse mouvement, i.e. you don't care about the cursor, the only way I know of is DirectInput, as Seroja said. I don't know any Win32 solution.

Good luck,
jods

Share on other sites
If you get the client coordinates of the mouse, you can check it against your form's ClientRectangle property to see if it was within the boundaries of your form. Is this what you mean by "outside the screen"?

1. 1
2. 2
Rutin
22
3. 3
A4L
15
4. 4
5. 5
khawk
14

• 13
• 26
• 10
• 11
• 44
• Forum Statistics

• Total Topics
633742
• Total Posts
3013639
×