robot | node window screenshot | move mouse | Search

The getScreenSize function is used to retrieve the current size of the screen, and it can be accessed from other JavaScript files by importing the module. The module is exported using module.exports = getScreenSize.

Run example

npm run import -- "get screen size"

get screen size


const robot = require("robotjs")

function getScreenSize() {
  return robot.getScreenSize()
}

module.exports = getScreenSize

What the code could have been:

const robot = require("robotjs");

/**
 * Get the current screen size.
 * @returns {Object} An object containing the screen width and height.
 */
function getScreenSize() {
  // Use object destructuring to simplify the return value
  const { width, height } = robot.getScreenSize();

  // Use object shorthand syntax to return the screen size directly
  return { width, height };
}

// Export the function as a default export
module.exports = getScreenSize;

Code Breakdown

Importing Module

const robot = require("robotjs")

getScreenSize Function

function getScreenSize() {
  return robot.getScreenSize()
}

Exporting Module

module.exports = getScreenSize