Neuron Powered Motorization

    keyboard-ts
    TypeScript icon, indicating that this package has built-in type declarations

    1.2.3 • Public • Published

    Keyboard

    A library for listening to keyboard shortcuts and combos.

    Example

    import { Keyboard } from 'keyboard-ts'
    import { Key } from 'keyboard-ts'
    
    const target = document.getElementById('target')
    const keyboard = new Keyboard(target)
    
    // listen to keys
    keyboard.on([ Key.Escape, Key.Delete ], () => {  /* on Del or Esc */ })
    keyboard.on([ Key.Space ], event => event.preventDefault())
    
    // listen to key combos
    keyboard.on([[ Key.Ctrl, Key.Z ]], () => {  /* on Ctrl+Z */ })
    keyboard.on([[ Key.Ctrl, Key.Shift, Key.Z ]], () => { /* on Ctrl+Shift+Z */ })
    keyboard.on([[ Key.Ctrl, Key.Z ], [ Key.Ctrl, Key.Y ]], () => { /* on Ctrl+Z or Ctrl+Y */ })
    
    // temporarily stop listening
    keyboard.stopListening()
    
    // start listening again
    keyboard.startListening()
    
    // stop listening forever, release all memory
    keyboard.clear()

    Caution

    // Do not use alert() as a callback!
    keyboard.on([ Key.Ctrl, Key.A ], () => {
        alert('stuff')
    
        // alert() erases the subsequent 'mouseup' event and this breaks the functionality
        // of the Keyboard class, because it has no way of knowing whether a key was released or not.
        // Sadly, this is how browsers work. It is not a bug in this library.
    })

    Install

        npm i keyboard-ts
        yarn add keyboard-ts
    

    Licence

    Feel free to use in any way. (MIT)

    Install

    npm i keyboard-ts

    DownloadsWeekly Downloads

    43

    Version

    1.2.3

    License

    MIT

    Unpacked Size

    7.26 kB

    Total Files

    10

    Last publish

    Collaborators

    • fingerartur