Skip to content

fxha/electron-localshortcut

 
 

Repository files navigation

electron-localshortcut

A library to register/unregister keyboard shortcuts locally to a BrowserWindow instance, without using a Menu. This is built to circumvent this Electron issue.

NOTE: This library must be initialized with the current keyboard layout that can be received from native-keymap (see usage below). The library isn't shipped with native-keymap because it's a native library that is managed by MarkText.


Patched version of electron-localshortcut that's used in MarkText to prevent the default keyboard event and to solve issues with non-US keyboards.


Installation

$ npm install --save @hfelix/electron-localshortcut

Development Setup

$ yarn install
$ yarn setup-dev

Usage

const { electronLocalshortcut } = require('electron-localshortcut');
const { BrowserWindow }  = require('electron');
const { getCurrentKeyboardLayout, getKeyMap } = require('native-keymap');

// Set the current users keyboard layout to translate keyboard events.
electronLocalshortcut.setKeyboardLayout(getCurrentKeyboardLayout(), getKeyMap());

const win = new BrowserWindow();
win.loadUrl('about://blank');
win.show();

electronLocalshortcut.register(win, 'Ctrl+A', () => {
  console.log('You pressed ctrl & A');
});

electronLocalshortcut.register(win, 'Ctrl+B', () => {
  console.log('You pressed ctrl & B');
});

electronLocalshortcut.register(win, ['Ctrl+R', 'F5'], () => {
  console.log('You pressed ctrl & R or F5');
});

console.log(
  electronLocalshortcut.isRegistered(win, 'Ctrl+A')
);

electronLocalshortcut.unregister(win, 'Ctrl+A');
electronLocalshortcut.unregisterAll(win);

Shortcuts behaviour

If you register a shortcut for a window, this module unregister the shortcut when the window is hidden, unfocused or minimized, and automatically restore them when the window is restored and focused again. This differ from native global-shortcuts because they doesn't interfere with other apps running on the same machine.

API

See JSDoc or example.

License

This project use parts of the following open-source projects licensed under The MIT License (MIT): atom-keymap, electron-localshortcut, electron-is-accelerator and keyboardevent-from-electron-accelerator. Please see LICENSE for more details.

About

Add keyboard shortcuts locally to a BrowserWindow instance, without using a Menu

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%