Created
October 24, 2021 05:10
-
-
Save zt4ff/741c98081f5802997fca1244b0a1b6bf to your computer and use it in GitHub Desktop.
Move desktop mouse and keyboard using NodeJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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