Logo Vincent
Back to all posts

Electron in Practice: Registering Shortcuts

Electron
Electron in Practice: Registering Shortcuts
Table of Contents

Preface

This article introduces how to register keyboard shortcuts in an Electron app.

Main Process

Registering shortcuts in the main process is straightforward — just make sure to do it after app ready.

Common key mappings: https://www.electronjs.org/docs/latest/api/accelerator

// Register
globalShortcut.register(shortcutKey, shortcutCallback);

// Unregister
globalShortcut.unregister(shortcutKey);

Also, remember to clean up all registered shortcuts when the app quits.

app.on("will-quit", () => {
  globalShortcut.unregisterAll();
});

Renderer Process

Registering shortcuts in the renderer process is a bit more involved.

An npm package has been created for this — feel free to use it: https://code.insistime.com/#/qiao-x-shortcut

Initialize IPC Listener in the Main Process

import { shortcutIPCInit } from "qiao-x-shortcut";

shortcutIPCInit({
  shortcutCallbackName: () => console.log("shortcutCallbackName from main process"),
});

Preload Setup

shortcut-preload.js

// electron
import { ipcRenderer } from "electron";

/**
 * shortcutGlobalIPC
 * @returns res
 */
export const shortcutGlobalIPC = async (shortcutKey, shortcutCallbackName) => {
  return await ipcRenderer.invoke("ipc-shortcut-global", shortcutKey, shortcutCallbackName);
};

preload.js

// electron
import { contextBridge } from "electron";

// custom preload
import { shortcutGlobalIPC } from "./shortcut-preload.js";

// preload
contextBridge.exposeInMainWorld("electron", {
  shortcutGlobalIPC,
});

Calling from the Renderer Process

await window.electron.shortcutGlobalIPC("Command+Control+X", "shortcutCallbackName");

Result

After registering the shortcut Command+Control+X in the renderer process,

the effect is as follows — each time the shortcut is pressed, the main process logs a message:

shortcutCallbackName from main process
shortcutCallbackName from main process
shortcutCallbackName from main process

Related Articles

Electron in Practice: Local Logging

Preface: After developing a local app with Electron, local logging is essential. It helps capture user actions and locate bugs. Local logs differ from real-time analytics — they are larger, more detailed, and not all information is suitable for reporting. Similarities and Differences with Node.js Logging: Writing local logs in Electron is similar to Node.js.

Electron in Practice: Update Strategies

Preface: After developing a desktop app with Electron, the next question is how to update subsequent versions. This article introduces several update strategies. Electron Architecture: An Electron desktop app consists of several parts: the Electron runtime itself, the main process (Node.js and non-Node.js parts), and the renderer process.

Electron in Practice: Customizing Mac Menus

Preface: Previous articles covered developing an Electron app through to Mac packaging. Related articles: Learn Electron in One Article, Electron in Practice: Local Database SQLite, Electron in Practice: Managing Electron Projects with Monorepo, Electron in Practice: Mac Packaging, Electron in Practice: Registering an Apple Developer Account.

Electron in Practice: Package Size Optimization

Preface The previous articles covered developing an Electron app through to Mac packaging. Related articles: Learn Electron in One Article, Electron in Practice: Local Database SQLite, Electron in Practice: Managing Electron Projects with Monorepo, Electron in Practice: Mac Packaging, Electron in Practice: Register

Electron in Practice: DMG Installer Customization

Preface A dmg file is a common installer format on Mac. The previous articles have already developed and packaged an application using Electron. This article focuses on customizing the dmg installer.

© 2026 vincentqiao.com . All rights reserved.