Ask Your Question
1

How to identify the screen location of the mouse pointer in an electron app?

asked 2023-03-30 11:00:00 +0000

bukephalos gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-12-09 18:00:00 +0000

huitzilopochtli gravatar image

You can use the global electron.screen object to retrieve information about the screen(s) on the user's device, and the electron.screen.getCursorScreenPoint() method to get the current position of the mouse pointer on the screen.

Here's an example code snippet that logs the screen position of the mouse pointer whenever the user clicks within your Electron app's main window:

const { screen } = require('electron');

// Get a reference to the main window
const mainWindow = // ...

// Attach click event listener to the main window
mainWindow.on('click', () => {
  // Get the current screen cursor position
  const { x, y } = screen.getCursorScreenPoint();

  console.log(`Mouse pointer location: (${x}, ${y})`);
});

You can customize this code to use other input events (mousemove, etc.) or to respond to events in specific UI elements within your app.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-03-30 11:00:00 +0000

Seen: 13 times

Last updated: Dec 09 '21