Giter Club home page Giter Club logo

shorte's Introduction

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

shorte's People

Contributors

imsnif avatar

Watchers

 avatar James Cloos avatar  avatar

Forkers

penggeddd

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.