In XUL or JavaScript, is there a way to move the mouse cursor to specified position?
In XUL or JavaScript, is there a way to move the mouse cursor to specified position?
1k views Asked by The Student At
3
There are 3 answers
0
On
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.
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.