[.net] Mouse delta

Started by
5 comments, last by DaWanderer 18 years, 11 months ago
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
You should never let your fears become the boundaries of your dreams.
Advertisement
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);
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).
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.
You should never let your fears become the boundaries of your dreams.
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.
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
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"?

This topic is closed to new replies.

Advertisement