How to use ioHook in electron application

Posted By : Ritik Jain | 03-Dec-2020

The content described in the following module can handle keyboard and mouse events via native hooks inside and outside your JavaScript/TypeScript application.

It is fully compatible with Electron. First, you have to install the dependencies of iohook given below-:

 

"dependencies": {

"iohook": "^0.6.5",

}

 

And also add iohook properties in package.json file:-

 "iohook": {
    "targets": [
      "node-64",
      "electron-69"
    ],
    "platforms": [
      "win32",
      "darwin",
      "linux"
    ],
    "arches": [
      "x64",
      "ia32"
    ]
  }

 

Now import iohook in your main.ts file:-

'use strict';

const ioHook = require('iohook');

ioHook.on('mouseup', event => {
  mouseMoveCount = mouseMoveCount+1;
  console.log("====Mouse===>>>",mouseMoveCount);
});
ioHook.on('keydown', event => {
  keyboardClickCount = keyboardClickCount+1;
  console.log("====keyboard===>>>",keyboardClickCount);
});

 

Note:- You can also add iohook in the index.html. It will work fine as well. 

If you want to handle iohook dynamically then you have to add two functions which are used to start and stop the iohook functionality. Functions are given below:-

ipcMain.on('onMouseAndKeyBoardCount', (event, data) => {
 ioHook.start();
 event.returnValue = { status: "SUCCESS", message: "Mouse and keyboard count On", data: { } };
})

ipcMain.on('offMouseAndKeyBoardCount', (event, data) => {
  ioHook.stop();
  event.returnValue = { status: "SUCCESS", message: "Mouse and keyboard count Off", data: { } };
 })

 

In this, we are using electron function ipcMain and ipcRenderer.

offMouseAndKeyBoardCount(taskData){
  return this._electronService.ipcRenderer.sendSync('offMouseAndKeyBoardCount',taskData);
}

saveMouseAndKeyBoardCount(taskData){
  return this._electronService.ipcRenderer.sendSync('saveMouseAndKeyBoardCount',taskData);
}

 

Thanks.

 

We are a 360-degree software development company that provides complete web and mobile app development solutions for varied project requirements. Our end-to-end SaaS app development services address your mission-critical project requirements through scalable, responsive, and feature-rich software applications that are easy to scale. We carefully analyze your project requirements and formulate effective strategies to build enterprise-grade web and mobile applications for multiple platforms. For more info, contact us at [email protected]

About Author

Author Image
Ritik Jain

Ritik is an accomplished Backend Developer with extensive experience in Mean. He is proficient in Core Java, Spring-Boot, Hibernate, Node.js, Angular 2+, and various relational databases like MySQL and MongoDB. With his expertise in API implementations, webservices, development testing, and deployments, he has contributed to the successful completion of various client projects. Apart from his professional pursuits, Ritik is an enthusiastic gamer and keeps himself updated with the latest advancements in technology.

Request for Proposal

Name is required

Comment is required

Sending message..