paint-brush
How to Act Like You're Working When You're not With RobotJSby@zt4ff
628 reads
628 reads

How to Act Like You're Working When You're not With RobotJS

by Kayode OluwasegunDecember 12th, 2021
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

This is for EDUCATIONAL purposes only 😄. We will be writing a similar program with NodeJS using the [RobotJS module]. RobotJS is a cross-platform desktop automation library. 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 (`/m) and define the time intervals of the events in seconds.

Company Mentioned

Mention Thumbnail
featured image - How to Act Like You're Working When You're not With RobotJS
Kayode Oluwasegun HackerNoon profile picture


A while ago, I saw a meme video of "a day in the life of a software engineer" where the engineer wrote a script to make his computer switch on automatically, open Slack, and move the mouse at regular intervals while he is asleep to make it appear he is online and working at the same time.


We will be writing a similar program with NodeJS using the RobotJS module. RobotJS is a cross-platform desktop automation library.


This is for EDUCATIONAL purposes only. 😊


https://cdn.hackernoon.com/images/ckx-29-v-0-sn-000-v-0-as-6-b-1-fj-01-hs.jpg

Steps

  • 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.

I wrote an article on parsing NodeJS CLI arguments here.


We will define boolean variables to ascertain what operations to perform:

let is_both;
let is_mouse;
let is_keyboard;


Next, we will define functions to move the mouse and press the keyboard

function moveMouseBackAndForth() {
	robot.moveMouseSmooth(200, 200);
  robot.moveMouseSmooth(400, 400);
}

function pressKeyBoard() {
  robot.keyTap("shift");
}


Then we will call the functions depending on the arguments passed. The whole code will look like this:

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.


Thanks for reading through. Would you rather have the program do something else than press the keyboard?'


You can get the code from this Github gist


I appreciate your feedback and questions.


First published here