Skip to content

Instantly share code, notes, and snippets.

@zt4ff
Created October 24, 2021 05:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save zt4ff/741c98081f5802997fca1244b0a1b6bf to your computer and use it in GitHub Desktop.
Save zt4ff/741c98081f5802997fca1244b0a1b6bf to your computer and use it in GitHub Desktop.
Move desktop mouse and keyboard using NodeJS
const yargs = require("yargs");
const robot = require("robotjs");
const { hideBin } = require("yargs/helpers");
let is_both;
let is_mouse;
let is_keyboard;
const arg = yargs(hideBin(process.argv))
.command("$0 [interval]", true, (yargs) => {
yargs
.positional("interval", {
type: "number",
describe: "the interval in second",
})
.default("interval", 60); // 60 seconds default
})
.usage("runs a desktop automator to run key your mmouse move at interval")
.example(
"$0 -mk 3",
"moves the mouse and press the keyboard after three seconds"
)
.option("m", {
description: "enable the mouse",
type: "boolean",
})
.option("k", {
description: "enable the keyboard",
type: "boolean",
})
.default("m", true)
.help("h").argv;
let { m, k, interval } = arg;
// multiply seconds by 1000 to get milliseconds
interval = interval * 1000;
if (m && k) is_both = true;
else {
if (m) is_mouse = true;
else if (k) is_keyboard = true;
}
function moveMouseBackAndForth() {
robot.moveMouseSmooth(200, 200);
robot.moveMouseSmooth(400, 400);
}
function pressKeyBoard() {
robot.keyTap("shift");
}
if (is_both) {
setInterval(() => {
moveMouseBackAndForth();
pressKeyBoard();
}, interval);
} else if (is_keyboard) setInterval(pressKeyBoard, interval);
else {
setInterval(moveMouseBackAndForth, interval);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment