Coder Social home page Coder Social logo

hot-key's Introduction

alt tag

Listens hotkeys events in browser.

  npm i hot-key

How to use? (JS)

const HotKey = require('hot-key')

const config = {

  // target node
  domNode: document.querySelector('.editor'),

  // delay after which pressing becomes non-active
  delay: 1000,

  // maximum number of combinations
  symbols: 3,

  // combinations rules
  rules: [
    {
      name: 'ctrl+z',
      codes: [17, 90],
    },
    {
      name: 'ctrl+shift+z',
      codes: [17, 16, 90],
    },
    {
      name: 'cmd+z',
      codes: [91, 90],
    },
    {
      name: 'cmd+shift+z',
      codes: [91, 16, 90],
    },
  ]

}


// create object
const hotKey = new HotKey(config)

// assign events
hotKey.on('ctrl+z', (event) => {

  console.log('history back')

  // in event.rule you can access to matched rule

})

hotKey.on('cmd+z', (event) => {

  console.log('history back')

})

// or like this
hotKey.on(['ctrl+shift+z', 'cmd+shift+z'], (event) => {

  console.log('history forward')

})

// also you can extend rules on the fly like this:
hotKey.setRules([
  ...hotKey.getRules()
  {
    name: 'cmd+a',
    codes: [91, 65],
  },
])

// start listen keyup and keydown events
hotKey.start()

// stop listen events
hotKey.stop()

How to use? (TS)

import { HotKey } from 'hot-key';

// ... mostly same as in JS

hotKey.on('cmd+z', (event: KeyboardEvent) => {
  console.log('history back');
})

hot-key's People

Contributors

fingerartur avatar sterzhakov avatar

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.