In XUL or JavaScript, is there a way to move the mouse cursor to specified position?
-
I'd hate to think of the chaos this would cause, if ad networks could control the mouse and force a click on ads. – Marc B Feb 10 '11 at 17:19
-
@Marc B: it's for a desktop app. – The Student Feb 10 '11 at 17:23
-
possible duplicate of [In Xul, within a transparent window, how do I hide the mouse cursor?](http://stackoverflow.com/questions/4960411/in-xul-within-a-transparent-window-how-do-i-hide-the-mouse-cursor) – Pointy Feb 10 '11 at 17:25
-
Why do you keep asking the same question? Just go back and update the original one. – Pointy Feb 10 '11 at 17:25
-
1@Pointy I made a question that actually needed to be slip into 2 questions. The question about hiding the mouse was answered correctly, so I wanted to mark the guy's answer as correct. Now I have other 2 questions: this about move the cursor, and the other about hiding cursor in a Xul transparent window.. – The Student Feb 10 '11 at 17:30
3 Answers
You can use nsIDOMWindowUtils.sendNativeMouseEvent(x, y, 0, 0, null)
to reposition the mouse cursor. Perhaps combined with window.screenX/Y
to work out where you should move the cursor to, since sendNativeMouseEvent
seems to treat the (x,y)
as absolute screen coordinates.
I haven't tested this method very thoroughly, so there could be caveats. I can't think of any myself.
I know this is an old question, but I have not seen this solution suggested anywhere before, and it's not exactly obvious.
I've only tested with Firefox v48 on Windows 7. Here, sendNativeMouseEvent
calls SetCursorPos
to perform the actual repositioning.

- 1,637
- 17
- 24
No. you cannot move a mouse cursor using javascript.
But you can do this.
Hide the cursor. Load an image shaped like cursor. Animate the image.

- 53,448
- 46
- 161
- 251
-
In my xul app I don't know how to hide it, see [this question](http://stackoverflow.com/questions/4960411/in-xul-within-a-transparent-window-how-do-i-hide-the-mouse-cursor). If using pure javascript I cannot move it, maybe there's something in Xul that will allow me to do it. – The Student Feb 10 '11 at 17:39
The only time that Gecko moves the mouse is on Windows for the snap-to-default-button effect. This is used by XUL dialogs and wizards. The backend code doesn't actually check that you're giving it a button; any XUL control works. The mouse is moved to the centre of the element, if that point is on-screen, and the window is active. Normally the code checks that the system cursor snapping is enabled, but there is a preference that overrides that.

- 54,642
- 8
- 60
- 72
-
-
1@Tom Brito Sorry, you'll have to write a binary component to invoke `gdk_display_warp_pointer` yourself. – Neil Feb 11 '11 at 21:09