shorte

1.0.1 • Public • Published

ShortE

Keyboard shortcut modes for Electron

Build Status Coverage Status Standard - JavaScript Style Guide

Description

This allows you to designate a 'leader' key, that enters the user into a different 'mode'. Upon entering the new mode:

  • Your pre-defined shortcuts are registered and available to the user.
  • When using one of those shortcuts, a debouncing timer starts, at the end of which the user will return to the previous mode.
  • The user can press the 'cancel' shortcut to return to the previous mode.

Installation

npm install shorte

Usage

const ShortE = require('shorte')
const { globalShortcut } = require('electron')
// ...
app.on('ready', () => {
  const shortcuts = new ShortE(
    globalShortcut, // pass the electron globalShortcut object
    'Ctrl+B', // the initial leader key, this can be changed later
    { // opts
      debounceTime: 500 // default 0
      cancel: 'Ctrl+A' // default is Esc
    }
  )
  shortcuts.register('W', () => console.log('W was pressed'))
  shortcuts.register('Alt+B', () => console.log('Alt+B was pressed'))
  // Pressing: Ctrl+B + <indefinite wait> + W will print 'W was pressed'
  // Pressing: Ctrl+B + <indefinite wait> + Alt+B will print'Alt+B was pressed'
  // Pressing: 
  // Ctrl+B + <indefinite wait> + W + <wait less than 0.5 seconds> + Alt+B 
  // will print 'W was pressed' and 'Alt+B was pressed'
})

API

contructor

const shortcuts = new ShortE(globalShortcut, leader, opts)

Cteates the shortcuts object and registers the leader key.

  • globalShortcut - the electron globalShortcut object
  • leader - the leader key (electron Accelerator format) (eg. 'Ctrl+B')
  • opts - optional object including:
    • debounceTime (in milliseconds) - default is 0
    • cancel - string for custom button - default is Esc

register

shortcuts.register(key, cb)

Registers a shortcut key. Once the leader key will be pressed, this shortcut will be available.

  • key: The key combination in electron Accelerator format. (eg. 'Alt+B')
  • cb: The function to be called when this key is pressed.

leader

shortcuts.leader(key)

Changes the leader key.

  • key: the new leader key

cancel

shortcuts.cancel(key)

Changes the cancel key.

  • key: the new cancel key

Accessors

debounceTime

console.log(shortcuts.debounceTime) // 500
shortcuts.debounceTime = 100
console.log(shortcuts.debounceTime) // 100

shortcuts

console.log(shortcuts.shortcuts) // { a: [ some function ], b: [ some other function] }
shortcuts.shortcuts.a = function () => {} // silently fail

active

// Press leader key
console.log(shortcuts.active) // true
// Press a shortcut, wait debounceTime
console.log(shortcuts.active) // false

Events

active

shortcuts.on('active', () => console.log('leader key pressed, shortcuts now available'))
// Press leader key
// 'leader key pressed, shortcuts now available

inactive

shortcuts.on('inactive', () => console.log('timeout reached, shortcuts unavailable'))
// Press leader key
// Use shortcut and wait debounce time, or press cancel key
// 'timeout reached, shortcuts unavailable'

Contributing

Yes please.

License

MIT

Package Sidebar

Install

npm i shorte

Weekly Downloads

1

Version

1.0.1

License

MIT

Last publish

Collaborators

  • imsnif