Open In App

Keyboard Events in ElectronJS

ElectronJS is an Open Source Framework used for building Cross-Platform native desktop applications using web technologies such as HTML, CSS, and JavaScript which are capable of running on Windows, macOS, and Linux operating systems. It combines the Chromium engine and NodeJS into a Single Runtime.

In web development, the jQuery Keyboard Events provide a convenient way by which we can record the keypresses of the keyboard in a sequential order. These events include key combinations, special keys and modifiers and are fired as soon as the key is on its way down, is pressed and is released. These events are very important in case we want to track or trigger some functionality on certain key presses. In addition to providing Accelerators and the globalShortcut module, Electron also provide us with a way by which we can record the keyboard Events using the Instance methods and event of the built-in BrowserWindow object and the webContents property. This tutorial will demonstrate Keyboard Events in Electron. 



We assume that you are familiar with the prerequisites as covered in the above-mentioned link. For Electron to work, node and npm need to be pre-installed in the system.



Example: Follow the Steps given in How to Find Text on Page in ElectronJS to setup the basic Electron Application. Copy the Boilerplate code for the main.js file and the index.html file as provided in the article. Also perform the necessary changes mentioned for the package.json file to launch the Electron Application. We will continue building our application using the same code base. The basic steps required to setup the Electron application remain the same. 
 

package.json: 

{
  "name": "electron-key",
  "version": "1.0.0",
  "description": "Key events in Electron",
  "main": "main.js",
  "scripts": {
    "start": "electron ."
  },
  "keywords": [
    "electron"
  ],
  "author": "Radhesh Khanna",
  "license": "ISC",
  "dependencies": {
    "electron": "^8.3.0"
  }
}

Output: At this point, our basic Electron Application is set up. Upon launching the application, we should see the following result
 

Keyboard Events in Electron: The BrowserWindow Instance and webContents Property are part of the Main Process. To import and use BrowserWindow in the Renderer Process, we will be using Electron remote module. 

index.html: Add the following snippet in that file. 




<h3>Key Events in Electron</h3>

index.js: Add the following snippet in that file. 
 




const electron = require("electron");
 
// Importing BrowserWindow from Main Process
// using Electron remote
const BrowserWindow = electron.remote.BrowserWindow;
const win = BrowserWindow.getFocusedWindow();
 
// let win = BrowserWindow.getAllWindows()[0];
 
win.webContents.on("before-input-event", (event, input) => {
    console.log(input);
});

The before-input-event of the webContents property works closely with the KeyboardEvent Web API. The KeyboardEvent describes a user interaction with the keyboard. It inherits the Instance method and properties of the UIEvent and the global Event object. The before-input-event Instance Event is emitted before dispatching the keydown and keyup events of the KeyboardEvent in the web page. This Instance event leverages the Constructor of the KeyboardEvent object. It returns the following parameters. 

To get the current BrowserWindow Instance in the Renderer Process, we can use some of the Static Methods provided by the BrowserWindow object. 

Output: At this point, we should be able to successfully trigger the KeyboardEvents in Electron. 
 


Article Tags :