How to Get the Mouse Position Without Events (Without Moving the Mouse)

How to get the mouse position without events (without moving the mouse)?

Real answer: No, it's not possible.

OK, I have just thought of a way. Overlay your page with a div that covers the whole document. Inside that, create (say) 2,000 x 2,000 <a> elements (so that the :hover pseudo-class will work in IE 6, see), each 1 pixel in size. Create a CSS :hover rule for those <a> elements that changes a property (let's say font-family). In your load handler, cycle through each of the 4 million <a> elements, checking currentStyle / getComputedStyle() until you find the one with the hover font. Extrapolate back from this element to get the co-ordinates within the document.

N.B. DON'T DO THIS.

Javascript mouse coordinates without event

No, such thing is impossible with javascript only and without events.

is there a way to find mouse current position on the screen without an event?

Javascript is a language to add client side functionality on user interaction. Therefore, a javascript code snippet is always run as response to an event.

I would suggest using the onmousemove event. The event would be raised each time the cursor mouse would change. Therefore, you will know the mouse position at all times.

Hope I helped!

How to get mouse X position of a mouse event of a div (and not of its children) with React 17?

This works as expected in react version 17.0.2

function Component() {
const onMouseMove = (e) => {
let rect = e.currentTarget.getBoundingClientRect();
let x = e.clientX - rect.left;
let y = e.clientY - rect.top;

console.log(x, y);
};
return (
<div onMouseMove={onMouseMove}>
<div>A</div>
<div>B</div>
<div>C</div>
</div>
);
}

Based on this answer and a comment on it, about using a currentTarget

How to get the mouse position without events (without moving the mouse)?

Real answer: No, it's not possible.

OK, I have just thought of a way. Overlay your page with a div that covers the whole document. Inside that, create (say) 2,000 x 2,000 <a> elements (so that the :hover pseudo-class will work in IE 6, see), each 1 pixel in size. Create a CSS :hover rule for those <a> elements that changes a property (let's say font-family). In your load handler, cycle through each of the 4 million <a> elements, checking currentStyle / getComputedStyle() until you find the one with the hover font. Extrapolate back from this element to get the co-ordinates within the document.

N.B. DON'T DO THIS.

How to get mouse position in jQuery without mouse-events?

I don't believe there's a way to query the mouse position, but you can use a mousemove handler that just stores the information away, so you can query the stored information.

jQuery(function($) {
var currentMousePos = { x: -1, y: -1 };
$(document).mousemove(function(event) {
currentMousePos.x = event.pageX;
currentMousePos.y = event.pageY;
});

// ELSEWHERE, your code that needs to know the mouse position without an event
if (currentMousePos.x < 10) {
// ....
}
});

But almost all code, other than setTimeout code and such, runs in response to an event, and most events provide the mouse position. So your code that needs to know where the mouse is probably already has access to that information...

how to get live mouse position while holding mousebutton

maybe you can try this?

let start = null
let end = null
let move = null
let isHold = false

document.addEventListener('mousedown', e => {
isHold = true
start = e
})
document.addEventListener('mouseup', e => {
isHold = false
end = e
console.log({start, end, move})
})
document.addEventListener('mousemove', e => {
if (isHold) {
move = e
}
})

Get element currently under mouse without using mouse events

I just looked through the source for code that gets (or stores and makes available) the cursor position. I didn't find anything one could use (from Javascript, XPCOM or not). I might have missed something... MXR is your friend.

However, if you want to avoid mousemove (and this is a good idea in general), you can just look for the innermost hovered element, e.g. like so.

function getInnermostHovered() {
var n = document.querySelector(":hover");
var nn;
while (n) {
nn = n;
n = nn.querySelector(":hover");
}
return nn;
}

(fiddle demoing the principle)

While this is what I'd consider a hack, it seems to work good enough most of the time, but will fail if the element has mouse events disabled via pointer-events. There could be other issues I didn't think of...

Of course, this can return nothing when the document has no hovered element (e.g. the mouse is not actually within the document).

Retrieve mouse position in JavaFX without event

Well, you could get mouse coordinates using Robot class. Here is an example.

com.sun.glass.ui.Robot robot =
com.sun.glass.ui.Application.GetApplication().createRobot();

int y = robot.getMouseY();
System.out.println("y point = " + y);
int x = robot.getMouseX();
System.out.println("x point= " + x);

It tried it on linux (elementary OS) and it works.

Update:
After some googling, I found TestFX which looks like an attempt to implement a prototype for Robot class. Have a look at links given below.
https://github.com/TestFX/Robot
http://mail.openjdk.java.net/pipermail/openjfx-dev/2015-December/018412.html

You can also do something like this, to get the coordinates.

public void start(Stage primaryStage) throws Exception {
GlassRobot robot = new GlassRobotImpl();
Point2D point = robot.getMouseLocation();
double x = point.getX();
double y = point.getY();
System.out.println("y = " + y);
System.out.println("x = " + x);
if(x > 10) {
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}


Related Topics



Leave a reply



Submit