visit
Run npm install yargs robotjs
to install required dependencies.
create an app.js
file and paste the code below. (I will explain the code):
// app.js
const yargs = require("yargs");
const { hideBin } = require("yargs/helpers");
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;
The code above configures the argument options our application needs and also defines a CLI interface to describe the application when you run node app.js -h
. We will have options to run only keyboard press (-k
), mouse move (-m
) or both (-mk
) and define the time intervals of the events in seconds.
let is_both;
let is_mouse;
let is_keyboard;
function moveMouseBackAndForth() {
robot.moveMouseSmooth(200, 200);
robot.moveMouseSmooth(400, 400);
}
function pressKeyBoard() {
robot.keyTap("shift");
}
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);
}
Run node app.js -m 3
to move our mouse only at an interval of 3 seconds.
First published