Ask Your Question

Revision history [back]

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.