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

Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us


Recent Post


  • The Role of IT Service Companies in Business Innovation

    In today’s dynamic corporate environment, information technology (IT) has emerged as a vital engine of innovation and growth. IT service providers are crucial in helping with the shift as organizations work to remain competitive and fulfill digital world demands. The relevance of information technology in organizations and the crucial help offered by IT service companies […]

  • How To Build a Caching System Through Redis in Node JS?

    In today’s world, you don’t want your application to be slow. If a website is taking too long to respond there might be a chance the user will not visit your application again and Boosting node js application. In this article, we are going to implement the caching system through Redis in NodeJS. But before […]

  • Revolutionizing Healthcare with Big Data: 5 Impactful Use Cases

    In this blog, we are going to understand Big Data and how it is going to revolutionize the healthcare industry. So, first, let’s know what big data is. So, big data is the large volume of data that is coming from businesses, organizations, and people at a very high speed. Big data contains a variety […]

  • The Rise of No-Code/Low-Code Platforms: Democratizing Software Development

    As we are witnessing a paradigm shift in technology, the way we develop software is also undergoing a significant transformation. Just imagine a world where you can design and develop software applications just as easily as you organize your wardrobe. This is what no-code/low-code platform promises, These platforms offer visual tools and seamless interfaces that […]

  • NLP: A Human Way To Talk With Computers

    In a world where dependency on technology is increasing day by day, our interaction with computers and machines has evolved drastically. It is seen very normally that the humans are talking to the computers in their own language and getting their work done. But haven’t we been taught that computers only understand machine language? Well, […]

  • How Quantum Computing is Transforming Software Development Progress?

    Hello there! In this blog, I’ll outline how quantum computing can be combined with software development to rethink how we have been creating programs up to this point and how it will completely change the software development progress and you’re going to know how quantum computing helps software development progress. But before I discuss how […]