Skip to content
kschluter edited this page May 1, 2016 · 43 revisions

Methods


Keyboard

.setKeyboardDelay(ms)

Sets the delay in milliseconds to sleep after a keyboard event. This is 10ms by default.

Arguments

  • ms - Time to sleep in milliseconds.

.keyTap(key, modifier)

Press a single key.

Arguments

  • key - Accepts backspace, enter, up, down, left, right, escape, delete, home, end, pageup, pagedown, and a-z.
  • modifier (optional, string or array) - Accepts alt, command (win), control, and shift.

.keyToggle(key, down, modifier)

Hold down or release a key.

Arguments

  • key - Accepts backspace, enter, up, down, left, right, escape, delete, home, end, pageup, pagedown, and a-z.
  • down - Accepts 'down' or 'up'.
  • modifier (optional, string or array) - Accepts alt, command (mac), control, and shift.

.typeString(string)

Arguments

  • string - The string to send.

.typeStringDelayed(string, cpm)

Arguments

  • string - The string to send.
  • cpm - Characters per minute.

Mouse

.setMouseDelay(ms)

Sets the delay in milliseconds to sleep after a mouse event. This is 10ms by default.

Arguments

  • ms - Time to sleep in milliseconds.

.moveMouse(x, y)

Moves mouse to x, y instantly.

Arguments

  • x
  • y

Examples

var robot = require("robotjs");

//Move the mouse to 100, 100 on the screen. 
robot.moveMouse(100, 100);

.moveMouseSmooth(x, y)

Moves mouse to x, y human like.

Arguments

  • x
  • y

.mouseClick(button, double)

Clicks the mouse.

Arguments

  • button (optional) - Accepts left, right, or middle. Defaults to left.
  • double (optional) - Set to true to perform a double click. Defaults to false.

Examples

var robot = require("robotjs");

robot.mouseClick();

.mouseToggle(down, button)

Toggles mouse button.

Arguments

  • down (optional) - Accepts down or up. Defaults to down.
  • button (optional) - Accepts left, right, or middle. Defaults to left.

Examples

var robot = require("robotjs");

robot.mouseToggle("down");

setTimeout(function()
{
    robot.mouseToggle("up");

}, 2000);

.getMousePos()

Gets the mouse coordinates.

Return

Returns an object with keys x and y.

Examples

var robot = require("robotjs");

var mouse = robot.getMousePos();
console.log("Mouse is at x:" + mouse.x + " y:" + mouse.y);

.scrollMouse(magnitude, direction)

Scrolls the mouse either up or down.

Arguments

  • magnitude - The amount to scroll.
  • direction - Accepts down or up.

Examples

var robot = require("robotjs");

robot.scrollMouse(50, "up");

setTimeout(function()
{
    robot.scrollMouse(50, "down");
}, 2000);

Screen

.getPixelColor(x, y)

Gets the pixel color at x, y. This function is perfect for getting a pixel or two, but if you'll be reading large portions of the screen use screen.capture.

Arguments

  • x
  • y

Return

Returns the hex color code of the pixel at x, y.

.getScreenSize()

Gets the screen width and height.

Return

Returns an object with .width and .height.

.screen.capture

Gets part or all of the screen.

Arguments

  • x (optional)
  • y (optional)
  • height (optional)
  • width (optional)

If no arguments are provided, screen.capture will get the full screen.

Return

Returns a bitmap object.

Bitmap

This is an object returned by screen.capture.

Properties

  • width - The width of the bitmap.
  • height - The height of the bitmap.
  • image - The raw image (buffer).
  • byteWidth
  • bitsPerPixel
  • bytesPerPixel

.colorAt

Gets the pixel color at x, y of a bitmap.

Arguments

  • x
  • y

Return

Returns the hex color code of the pixel at x, y.

Examples

var robot = require("robotjs");

//Get a 100x100 screen capture starting at 0, 0. 
var img = robot.screen.capture(0, 0, 100, 100);

console.log(img.width)

//Get pixel color at 50, 50.
var hex = img.colorAt(50, 50);
console.log(hex);
Clone this wiki locally