36

I have a link that, when clicked, I would like it to move the position of the mouse to the right (or anywhere within the viewport, for that matter).

in code it would probably look similar to the following:

$('a#expand').click(function(e){
    $(document)
       .mouseXPos(e.pageX + 50)
       .mouseYPos(e.pageY + 50);
});

Chaining might not be necessary, of course, but a similar 'set mouse position' functionality is what I am after.

I've seen solutions to move the cursor position to a certain spot in the text, but I didn't glean much from them.

animuson
  • 53,861
  • 28
  • 137
  • 147
Michael
  • 1,786
  • 5
  • 23
  • 42
  • 1
    This sounds like something that will really irritate users! I hope you have a good reason for doing this, not that it's possible. – ScottE Jul 30 '09 at 19:28
  • I should explain further. I'm using Brian's great hoverIntent plugin for jQuery, along with a menu that includes a linked tab that expands the menu. I'm animating the sliding out, but the hoverIntent misfires the hover off if the mouse stays still while the menu is animating. Sounds odd but this plugin catches all of the rest of the classic mouseenter/leave misfires that devs typically run across. He knows about this issue and is working on it for the next release. Being able to somehow move the mouse one pixel over would be great. Either client-side or server-side solution would be awesome. – Michael Aug 03 '09 at 14:28
  • 2
    while this sounds at first blush like something that would irritate users, let me describe a situation where _not_ moving the mouse would irritate users and moving it might actually please them... imagine that you click something on a web page and the elements on the page rearrange; if the mouse has not moved, you are no longer over the element on which you clicked; the mouse has not moved relative to the page but it _has_ moved relative to the *content*. "moving" the mouse relative to the page could be required in order to provide the experience that the mouse has not moved. – David Alpert Apr 16 '10 at 18:46

6 Answers6

58

There is no mechanism for moving the mouse via JavaScript.

MyItchyChin
  • 13,733
  • 1
  • 24
  • 44
  • Any mechanism for ASP.Net, AJAX, jQuery, C#? I know the mouse position can be read via jQuery, maybe there's a way to expose it via the DOM? – Michael Aug 03 '09 at 14:33
  • 5
    @Michael: No, the mouse position is read-only for JavaScript. AJAX is just method of communication for JavaScript executing on the client to talk to a server Application via HTTP. ASP.NET/C# is server side and it's scope is limited to the server it executes on. The only way to control a client's mouse is with a client side application that is running in user space which means getting them to download and install something. – MyItchyChin Aug 03 '09 at 15:24
  • 1
    Although this is quite a late response, thank you CptSkippy. I upvoted and marked as the answer for your comment to my question. – Michael Feb 01 '11 at 19:28
  • If you get a moment, and I would think it overkill, but could Silverlight perform the action, since the XAP and application lives on the client? – Michael Feb 01 '11 at 19:30
  • @Michael: No, Silverlight cannot run unmanaged code and the only way to control the mouse would be via the User32.dll. If the Silverlight app were out-of-browser it could call COM which could wrap around user32.dll but I don't think that's what you had in mind at all. – MyItchyChin Feb 03 '11 at 15:30
11

I may be wrong, but I don't think it's possible to move the mouse pointer from client-side script. Given the potential for abuse, I certainly hope it isn't.

Jason Musgrove
  • 3,574
  • 19
  • 14
  • I had definitely thought about the worry about this. Should I ever come across such a power, I'll be sure to find a solution to it and let you know. Do you know of a server-side script to do this? Thanks. – Michael Aug 03 '09 at 14:30
  • @Michael: Server-side script is even less likely to move the client's mouse pointer. It *may* be possible to move the mouse pointer via ActiveX control (Internet Explorer only), custom plugin, .NET control, or possibly by signed Java applet. – Jason Musgrove Aug 04 '09 at 14:48
4

There's no way to accomplish mouse position change via JavaScript or any Client-Side Script. The only reason for that is not to give a client side script potential for abuse as stated before.

George
  • 1,466
  • 3
  • 12
  • 30
4

You could hide the cursor, and show another one at a different place.

Good to have when moving around in a maze for example. The cursor looks like it's stopped but you will see it again when you move outside the window.

Zuul
  • 41
  • 1
  • 1
  • may I know how is it possible as I need to implement it? – heyanshukla May 08 '12 at 05:28
  • 1
    @heyanshukla To hide [a cursor](http://www.w3schools.com/cssref/pr_class_cursor.asp), you can use `document.body.style.cursor = url('empty.ico');` if you have some empty icon file. Or you can set `document.body.style.cursor = 'none';` but this works only in Firefox. Of course it cannot customize mouse coordinates, only can hide the cursor. – Stano Aug 12 '12 at 19:55
2

As other users already have mentioned, there isn't any mechanism is Javascript to do that. However, you can disable the mouse and implement a cursor to do what you need. Here is a link that explains how. How to implement a custom cursor.

mahsa.teimourikia
  • 1,664
  • 9
  • 32
  • 64
0

You map change scroll position that will automatically move your pointer to required position;

$(document).scrollTop();

For some case, I was required to stay pointer on same checkbox although a button show/hide was causing a bubbling... so I did something like;

$(document).scrollTop( $(document).scrollTop() + parseInt($('.btn-show-selected-group').outerHeight()) );
$(document).scrollTop( $(document).scrollTop() - parseInt($('.btn-show-selected-group').outerHeight()) );