ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] > [官网](https://robotjs.io/docs/) ## 概述 Robotjs是nodejs的第一个用于桌面自动化的库。他能自动化鼠标、键盘和读取屏幕,并且提供了Mac, Windows, and Linux的跨平台支持 ## 安装 `npm install robotjs` ## 示例 ## [Mouse](https://robotjs.io/docs/syntax#mouse) ``` // Move the mouse across the screen as a sine wave. var robot = require("robotjs"); // Speed up the mouse. robot.setMouseDelay(2); var twoPI = Math.PI * 2.0; var screenSize = robot.getScreenSize(); var height = (screenSize.height / 2) - 10; var width = screenSize.width; for (var x = 0; x < width; x++) { y = height * Math.sin((twoPI * x) / width) + height; robot.moveMouse(x, y); } ``` ## [Keyboard](https://robotjs.io/docs/syntax#keyboard) ``` // Type "Hello World" then press enter. var robot = require("robotjs"); // Type "Hello World". robot.typeString("Hello World"); // Press enter. robot.keyTap("enter"); ``` ## [Screen](https://robotjs.io/docs/syntax#screen) ``` // Get pixel color under the mouse. var robot = require("robotjs"); // Get mouse position. var mouse = robot.getMousePos(); // Get pixel color in hex format. var hex = robot.getPixelColor(mouse.x, mouse.y); console.log("#" + hex + " at x:" + mouse.x + " y:" + mouse.y); ```