Shortkey Parser

Keyboard shortkey parser for JavaScript.

View the Project on GitHub

This is a library for parsing human-readable keyboard shortkeys in JavaScript.

Installation

npm install shortkey-parser

Usage

shortkey-parser is exposing a parse method, which can be used to parse shortkey strings.

const { parse } = require('shortkey-parser');

const shortkeys = parse('ctrl+k, a');

const json = shortkeys.toJSON();

Definitions

Key

Key represents a single key on the keyboard. It has a type of KeyType which is represented as a most basic object in shortkey-parser.

Keys are represented in a flat object. As an example, this is a A key on the keyboard:

// key A on keyboard
{
    which: 65,
    keyCode: 65,
    code: 'KeyA',
    key: 'a',
    location: 0,
},

KeyGroup

KeyGroups are groups of keyboard keys. Each Key has its own group and some keys are grouped under same alias names:

// Left alt key on keyboard
'left-alt': [
    {
        which: 18,
        keyCode: 18,
        code: 'AltLeft',
        key: 'Alt',
        location: 1,
    },
],

// alt Keys
'alt': [
    {
        which: 18,
        keyCode: 18,
        code: 'AltLeft',
        key: 'Alt',
        location: 1,
    },
    {
        which: 18,
        keyCode: 18,
        code: 'AltRight',
        key: 'Alt',
        location: 2,
    },
]

KeyGroup has methods below:

Shortkey

Shortkeys are combination of KeyGroups separated by “+”. Here is an example of alt+d Shortkey.

// "alt+d" Shortkey
[
    [
        {
            which: 18,
            keyCode: 18,
            code: 'AltLeft',
            key: 'Alt',
            location: 1,
        },
        {
            which: 18,
            keyCode: 18,
            code: 'AltRight',
            key: 'Alt',
            location: 2,
        },
    ],
    [
        {
            which: 68,
            keyCode: 68,
            code: 'KeyD',
            key: 'd',
            location: 0,
        },
    ]
]

In our convention, you are not tied to modifiers like alt and only one general key. Here is an example of a+d Shortkey.

// "a+d" Shortkey
[
    [
        {
            which: 65,
            keyCode: 65,
            code: 'KeyA',
            key: 'a',
            location: 0,
        },
    ],
    [
        {
            which: 68,
            keyCode: 68,
            code: 'KeyD',
            key: 'd',
            location: 0,
        },
    ]
]

Shortkey has methods below:

ShortkeySequence

ShortkeySequences are list of Shortkeys separated by “,”. Here is an example of ctrl+k, d Shortkey.

// "ctrl+k, d" ShortkeySequence
[

    [
        [
            [
                {
                    which: 17,
                    keyCode: 17,
                    code: 'ControlLeft',
                    key: 'Control',
                    location: 1,
                },
            ],
            [
                {
                    which: 17,
                    keyCode: 17,
                    code: 'ControlRight',
                    key: 'Control',
                    location: 2,
                },
            ]
        ],
        [
            {
                which: 75,
                keyCode: 75,
                code: 'KeyK',
                key: 'k',
                location: 0,
            },
        ]
    ],
    [
        [
            {
                which: 68,
                keyCode: 68,
                code: 'KeyD',
                key: 'd',
                location: 0,
            },
        ]
    ]

]

ShortkeySequence has methods below:

Special Cases

Whitespaces

All whitespaces are ignored.

Special Characters

You can put special characters inside quotes or double quotes. Also remember that always in this cases there is an equivalent alias.

alt+'+'
alt+plus

Any Key can be put inside a quote or double quote. If you are not sure you need one, you can always put it in quotes or use an alias.

Keys and Aliases

Click on each key to get the value to be used in the Shortkey. You can view other aliases for the keys inside KeyMaps file.

Escape F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12 Back Quote 1 2 3 4 5 6 7 8 9 0 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z Tab CapsLock Left Shift Right Shift Left Control Right Control Left Alt Right Alt Left Meta Right Meta Space Page Up Page Down End Home Delete Left Up Right Down Numlock Numpad Divide Numpad Multiply Numpad Subtract Numpad Add Numpad Enter Numpad Period Numpad 0 Numpad 1 Numpad 2 Numpad 3 Numpad 4 Numpad 5 Numpad 6 Numpad 7 Numpad 8 Numpad 9 General Subtract Equal Backspace Bracket Left Bracket Right Backslash Semicolon Quote General Enter Comman General Period General Divide