Move the Mouse Pointer to a Specific Position

Move the mouse pointer to a specific position?

So, I know this is an old topic, but I'll first say it isn't possible. The closest thing currently is locking the mouse to a single position, and tracking change in its x and y. This concept has been adopted by - it looks like - Chrome and Firefox. It's managed by what's called Mouse Lock, and hitting escape will break it. From my brief read-up, I think the idea is that it locks the mouse to one location, and reports motion events similar to click-and-drag events.

Here's the release documentation:
FireFox: https://developer.mozilla.org/en-US/docs/Web/API/Pointer_Lock_API
Chrome: http://www.chromium.org/developers/design-documents/mouse-lock

And here's a pretty neat demonstration: http://media.tojicode.com/q3bsp/

changing position relative to mouse pointer

As per your question, the issue is that you're adding a new span everytime the mouse moves. I am guessing you only want to add it when mouse enters, and remove it once the mouse leaves.

I have tried to keep your code as much as possible and change only what is causing your issue.

document.querySelector("li a").addEventListener("mouseenter", (event) => {
let span; let alreadyHasSpan = event.target.childNodes.length > 1; //default length=1
if (!alreadyHasSpan) { span = $(`<span class="a-effect"></span>`); span.appendTo(event.target); }});
document.querySelector("li a").addEventListener("mousemove", (event) => { let targetLeftPos = event.target.getBoundingClientRect().left; if (event.target.childNodes.length > 1) { let span = event.target.childNodes[1]; $(span).css("left", `${event.clientX-targetLeftPos}px`); }});
document.querySelector("li a").addEventListener("mouseleave", (event) => { if (event.target.childNodes.length > 1) { let span = event.target.childNodes[1]; span.remove(); }});
a {  position: relative;}
.a-effect { position: absolute; left: 0; height: 100%; width: 2%; background: black;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<li class="nav-item active"> <a href="/" class="nav-link">Product</a></li>

Move the mouse pointer in a specific direction with unity

With the new input system (install via packagemanager) you can use
Mouse.current.WarpCursorPosition(...) (Input System version 1.0.0 as of nov 2020, or alternatively 1.1.0 preview 2).

https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/Mouse.html:

On desktop platforms (Windows, Mac, Linux, and UWP), you can move the mouse cursor via code. Note that this moves the system's actual mouse cursor, not just Unity's internally-stored mouse position. This means that the user sees the cursor jumping to a different position, which is generally considered to be bad UX practice. It's advisable to only do this if the cursor is hidden (see the Cursor API documentation for more information).



Related Topics



Leave a reply



Submit