robotjs README

The project's README from github.com/octalmage/robotjs. As one markdown file: README.md.

Node.js Desktop Automation. Control the mouse, keyboard, and read the screen.

RobotJS supports Mac, Windows, and Linux.

This is a work in progress so the exported functions could change at any time before the first stable release (1.0.0). Ideas?

Check out some of the cool things people are making with RobotJS! Have your own rad RobotJS project? Feel free to add it!

Contents

Installation

Install RobotJS using npm:

npm install robotjs

You can get npm here if you don't have it installed.

Published packages include Node-API prebuilds for Linux, macOS, and Windows on x64 and arm64. Other targets fall back to a source build.

If you need to build RobotJS, see the building section. Instructions for Electron.

Examples

Mouse

// Move the mouse across the screen as a sine wave.
const robot = require("robotjs");

// Speed up the mouse.
robot.setMouseDelay(2);

const twoPI = Math.PI * 2;
const screenSize = robot.getScreenSize();
const height = (screenSize.height / 2) - 10;
const width = screenSize.width;

for (let x = 0; x < width; x++) {
	const y = height * Math.sin((twoPI * x) / width) + height;
	robot.moveMouse(x, y);
}
Keyboard
// Type "Hello World" then press enter.
const robot = require("robotjs");

// Type "Hello World".
robot.typeString("Hello World");

// Press enter.
robot.keyTap("enter");
Screen
// Get pixel color under the mouse.
const robot = require("robotjs");

// Get mouse position.
const mouse = robot.getMousePos();

// Get pixel color in hex format.
const hex = robot.getPixelColor(mouse.x, mouse.y);
console.log(`#${hex} at x:${mouse.x} y:${mouse.y}`);
const robot = require("robotjs");

const screen = robot.screen.capture();
const target = robot.image.load("./target.bmp");
const match = screen.findImage(target, { tolerance: 0.1 });

if (match) {
	screen.click(match, target);
}

Captured and loaded images also provide findImages, countImage, findColor, findColors, countColor, colorAt, and save. Image searches return the target's top-left capture coordinates. click converts those coordinates to screen coordinates and clicks the target's center. Read the Wiki for more information!

API

The RobotJS API is hosted at https://robotjs.dev/docs/syntax.

macOS permissions

getAccessibilityPermission() and getScreenCapturePermission() report the current grants. requestAccessibilityPermission() and requestScreenCapturePermission() trigger the corresponding macOS system prompts. macOS still requires the user to approve each request. The Accessibility prompt is asynchronous, so requestAccessibilityPermission() returns the current grant. Check getAccessibilityPermission() again after the user responds.

Building

Please ensure you have the required dependencies before installing:

BMP image loading and saving is always available. PNG is enabled in all published prebuilds: Windows uses Windows Imaging Component, while macOS and Linux include statically linked libpng. PNG remains optional for macOS and Linux source builds. To enable it, install libpng and pkg-config, then force a source build with ROBOTJS_ENABLE_PNG=1. Check robot.image.supportsPNG at runtime.

Install node-gyp using npm:

npm install -g node-gyp

Then build:

node-gyp rebuild

See the node-gyp readme for more details.

Packaging prebuilds

The Prebuilds workflow builds the release binaries and uploads a complete npm-package artifact. It does not publish to npm. Download the artifact, then publish it manually:

npm publish ./robotjs-<version>.tgz

Plans

Progress

Module Status Notes
Mouse 100% All planned features implemented.
Keyboard 100% All planned features implemented.
Screen 100% Screen capture, image search, and pixel search.
Bitmap 100% BMP I/O and optional PNG support.

FAQ

Does RobotJS support global hotkeys?

Not currently, and I don't know if it ever will. I personally use Electron/NW.js for global hotkeys, and this works well. Later on I might add hotkey support or create a separate module. See #55 for details.

Can I take a screenshot with RobotJS?

Yes. robot.screen.capture() captures the main display. Pass x, y, width, height to capture a specific rectangle.

Why is <insert key> missing from the keyboard functions?

We've been implementing keys as we need them. Feel free to create an issue or submit a pull request!

How about multi-monitor support?

Use robot.getDisplays() to inspect displays and pass a rectangle from one display to robot.screen.capture(x, y, width, height). A capture rectangle cannot span multiple displays. Linux reports the current X11 screen.

For any other questions please submit an issue.

Story

I'm a huge fan of AutoHotkey, and I've used it for a very long time. AutoHotkey is great for automation and it can do a bunch of things that are very difficult in other languages. For example, it's imagesearch and pixel related functions are hard to reproduce on Mac, especially in scripting languages. These functions are great for automating apps that can't be automated like Netflix. This has never been a big deal since I've always used Windows at work, but for the past few years I've been using Mac exclusively.

I like AutoHotkey, but I like Node.js more. By developing RobotJS I get an AutoHotkey replacement on Mac (finally!), and I get to use my favorite language.

TLDR: There's nothing like AutoHotkey on Mac, so I'm making it.

License

MIT

Based on autopy. Maintained by Jason Stallings.

robotjs (octalmage/robotjs)

Searching Database…

12.8K ·GitHub
这是什么?RobotJS 是一个用于桌面自动化的 Node.js 库:它可以控制鼠标和键盘,并读取屏幕上的像素。
这是独立应用,还是我可以加入 Node 项目中的库?它是一个原生 Node.js 库(用 C 编写的 Node-API 插件),你需要在自己的项目中通过 require 引入,而不是独立应用。
如何把它添加到项目中?使用 npm 安装即可。预编译的 Node-API 二进制文件已包含在内,支持 x64 和 arm64 架构的 Linux、macOS 和 Windows,因此在这些平台上无需构建步骤。
安装前是否需要编译器或其他工具?只有在预编译版本不支持你的平台时才需要:Windows 需要 Visual Studio C++ 工具链,macOS 需要 Xcode Command Line Tools,Linux 需要 Python 3、make、C/C++ 编译器以及 libxtst-dev。
有没有开箱即用时无法工作的功能,比如全局快捷键或跨显示器截图?不支持全局快捷键(作者为此使用 Electron/NW.js),单个截屏区域也无法跨越多个显示器。在 macOS 上,还需要通过系统提示授予辅助功能和屏幕录制权限。
它可以在哪些操作系统上运行?RobotJS 支持 Mac、Windows 和 Linux。
它实际上能对屏幕上的图像和颜色做什么?它可以通过 findImages、countImage、findColor、findColors、countColor 和 colorAt 等函数,在截取的屏幕截图中查找图像和颜色;还可以将位图截图加载或保存为 BMP 格式(如果启用,也支持 PNG)。
它支持 PNG 截图吗?所有已发布的预编译版本都启用了 PNG(使用 Windows Imaging Component 或静态链接的 libpng)。对于 macOS/Linux 的源码构建,PNG 是可选的:需先安装 libpng 和 pkg-config,并设置 ROBOTJSENABLEPNG=1 进行构建。
我能只截取屏幕的一部分吗?可以。robot.screen.capture() 默认截取主显示器,但你也可以传入 x、y、width、height 来截取指定的矩形区域,还可以通过 robot.getDisplays() 指定某个显示器进行截取。
作者为什么要开发 RobotJS,而不直接用 AutoHotkey?作者喜欢 AutoHotkey,但后来全职改用 Mac,而 Mac 上没有同类工具。因此他开发了 RobotJS,把类似 AutoHotkey 的自动化能力带到 Node.js 中,并支持多个平台,尤其是 macOS。
Creator
octalmage
Size
1.2 MB
Stars
12.8K
Age
12y
Commits · 90 Days