Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Electron global-shortcut module


May 25, 2021 Electron


Table of contents


global-shortcut module can easily set (register/log off) shortcuts for various custom actions.

Note : Shortcuts registered with this module are system-global (QQ screenshot type), do not use this module (register shortcuts) before the app module responds to ready messages.

var app = require('app');
var globalShortcut = require('electron').globalShortcut;

app.on('ready', function() {
  // Register a 'ctrl+x' shortcut listener.
  var ret = globalShortcut.register('ctrl+x', function() {
    console.log('ctrl+x is pressed');
  })

  if (!ret) {
    console.log('registration failed');
  }

  // Check whether a shortcut is registered.
  console.log(globalShortcut.isRegistered('ctrl+x'));
});

app.on('will-quit', function() {
  // Unregister a shortcut.
  globalShortcut.unregister('ctrl+x');

  // Unregister all shortcuts.
  globalShortcut.unregisterAll();
});

Methods

global-shortcut contains the following functions:

globalShortcut.register(accelerator, callback)

  • accelerator Accelerator
  • callback Function

Sign accelerator shortcuts. The callback function is called when the user presses callback shortcut.

globalShortcut.isRegistered(accelerator)

  • accelerator Accelerator

Querying whether the accelerator shortcut has been registered will return true (registered) or false (not registered).

globalShortcut.unregister(accelerator)

  • accelerator Accelerator

Sign out of the global accelerator .

globalShortcut.unregisterAll()

Sign out of all global shortcuts registered by this app.