runi
runi

Reputation: 13

Is it possible to get the edit cursor position of any focused editor globally using JavaScript or some plugin?

There are some npm plugins to get the mouse position like robotjs, but I could not find anything similar to get the edit cursor position globally.

I am working on a desktop application for windows using electron framework and my requirement is that it should be able to show some menu or window below the edit cursor position. And the cursor could be in any text editor.

Upvotes: 1

Views: 1214

Answers (2)

Joshua
Joshua

Reputation: 5322

You can use the screen.getCursorScreenPoint() function like this:

var electron       = require('electron');
var cursorPosition = electron.screen.getCursorScreenPoint();

console.log('x: ' + cursorPosition.x);
console.log('y: ' + cursorPosition.y);

And that will print out the absolute x and y position of the mouse

Docs for screen.getCursorScreenPoint()

Upvotes: 1

JazzBrotha
JazzBrotha

Reputation: 1748

You can get the position of the cursor on mouse events using clientX and clientY

function showCoords(e) {
    const x = e.clientX;
    const y = e.clientY;
    const coords = `X: ${x}, Y: ${y}`
    document.getElementById("coords").innerHTML = coords;
}
<h2 onclick="showCoords(event)">Click this heading to get coords</h2>

<p id="coords"></p>

Upvotes: 0

Related Questions