Automating Desktop using RobotJS

RobotJS package gives users ability to control mouse and keyboard of the desktop. In our demo, we will try to draw a circle in MS Paint by controlling mouse actions. Here we will be using version 0.47 of RobotJS.

Development Environment:

  1. JetBrains Webstorm
  2. NPM 5.5.1

We will start by creating a simple node application in WebStorm.

Installing RobotJS:

Enter following command in terminal:

npm install robotjs --save

WebStorm creates a lot of boilerplate for us.

Next, we will set a route in our app.js as ‘draw’ where we will be adding our actions to draw different types of shapes.

... ... var index = require('./routes/index'); var users = require('./routes/users'); var draw = require('./routes/draw'); ... ... app.use('/', index); app.use('/users', users); app.use('/draw', draw); ... ...
Code language: PHP (php)

Next, we will create ‘draw.js’ file in ‘routes’ directory of the project.

We will be using the polar form of the circle ie.:

x = R * cosθ y = R * sinθ

for drawing the points of the circle by using angles ranging from 0-360.

Robot class provides following methods for keyboard and mouse events:

  1. robot.keyTap(“tab”, “alt”) -> Performs a single keypress (first argument is a key and second argument is a modifier).
  2. robot.getScreenSize() -> Returns the size of the desktop screen.
  3. robot.setMouseDelay(0) -> Sets the delay in milliseconds to sleep after a mouse event.
  4. robot.moveMouse(x, y) -> Moves mouse to x, y human like, with the mouse button up.
  5. robot.mouseClick(“left”, false) -> Clicks the mouse (first argument is the mouse key and second argument is for double key press).

The draw.js class is as below:

var express = require('express'); var robot = require("robotjs"); var router = express.Router(); router.get('/circle', function(req, res) { robot.keyTap("tab", "alt"); var screenSize = robot.getScreenSize(); var height = screenSize.height; var width = screenSize.width; var centerX = width / 2; var centerY = height / 2; var radius = Math.min(width, height) / 5; robot.setMouseDelay(0); for (var angle = 0; angle < 360; angle += 0.25) { var x = centerX + (radius * Math.cos(toDegrees(angle))); var y = centerY + (radius * Math.sin(toDegrees(angle))); robot.moveMouse(x, y); robot.mouseClick("left", false); } res.send('Bot at work'); }); function toDegrees (angle) { return angle * (180 / Math.PI); } module.exports = router;
Code language: JavaScript (javascript)

Final Drawing:

Circle drew by Robot events





Recent Post

  • Advantages of Permissioned Blockchains for Efficiency, Security, and Collaboration

    In the last decade, blockchain has emerged as a robust technology in the digital landscape. Blockchains are continuously transforming various industries by redefining data management, data security, and decentralized collaboration. Blockchain gained popularity with the emergence of cryptocurrencies. Let’s take a look back to the year 2017 when Japan recognized Bitcoin as a legal currency […]

  • How AI Is Revolutionizing Mobile App Development in 2024?

    Introduction In a world where smartphones have become an extension of our lifestyle, mobile applications have also become a major part of our daily routines. From making shopping effortless to booking a doctor’s appointment at our fingertips, from getting our food and groceries delivered to our doorstep to managing our finances and making instant transactions, […]

  • A Comprehensive Guide to Sentiment Analysis Using NLP

    Businesses need to understand public interests, attitudes, behavior, and trigger points in today’s dynamic and competitive market. This enables them to efficiently serve their customers, grab opportunities, grow, and develop resilience in the face of a constantly shifting market. Many businesses find it challenging to process vast amounts of text-based data in order to get […]

  • How AI Is Revolutionizing Banking: Transforming Customer Experiences and Enhancing Financial Security

    Banking is a huge industry with a global Banking market likely to achieve a Net Interest Income of USD 10.34 trillion, with Traditional Banks holding a huge stake of USD 8.30 trillion. According to Statista’s projections suggest an annual growth rate of 4.82% (CAGR 2024-2028), culminating in a market volume of USD12.48 trillion by 2028. […]

  • Mastering Hyperparameter Tuning in Python: Strategies, Techniques, and Tools for Model Optimization

    Understanding various aspects of deep learning and machine learning can often feel like stepping into uncharted territory with no clue where to go. As you start exploring various algorithms and data, you realize that success is based on more than just building a raw model, it’s more about fine-tuning it to perfection. And when we […]

  • What is Transfer Learning? Exploring The Popular Deep Learning Approach

    Have you ever thought about how quickly your smartphone recognizes faces in photos or suggests text as you type? Behind these features, there’s a remarkable technique called Transfer Learning that expands the capabilities of Artificial Intelligence. Now you must be wondering- What is Transfer Learning ? Picture this: Instead of starting from the square from […]

Click to Copy