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