All Projects → ianstormtaylor → Is Hotkey

ianstormtaylor / Is Hotkey

Licence: mit
Check whether a browser event matches a hotkey.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Is Hotkey

react-keyboard-shortcuts
A declarative library for handling hotkeys based on explicit priority in React applications
Stars: ✭ 23 (-89.1%)
Mutual labels:  keyboard, events, hotkeys
Chordly
Chordly is a javascript library that may be used to detect and act upon key sequences entered by a user.
Stars: ✭ 14 (-93.36%)
Mutual labels:  keyboard, events, hotkeys
g910-gkey-macro-support
GKey support for Logitech G910 Keyboard on Linux
Stars: ✭ 85 (-59.72%)
Mutual labels:  keyboard, hotkeys
LowLevelInput.Net
A thread safe and event driven LowLevelMouse and LowLevelKeyboard Hook
Stars: ✭ 32 (-84.83%)
Mutual labels:  keyboard, events
React Hotkeys
React component to listen to keydown and keyup keyboard events, defining and dispatching keyboard shortcuts.
Stars: ✭ 279 (+32.23%)
Mutual labels:  hotkeys, keyboard
hotkey
⌨️ cross-platform hotkey package
Stars: ✭ 82 (-61.14%)
Mutual labels:  keyboard, hotkeys
input-event
🎹 Read and parse input device(like mouse, keyboard, joystick and IR-Remote)'s event data.
Stars: ✭ 45 (-78.67%)
Mutual labels:  keyboard, events
Vimperator Labs
Vimperator
Stars: ✭ 1,184 (+461.14%)
Mutual labels:  keyboard, browser
Inputsystem
An efficient and versatile input system for Unity.
Stars: ✭ 1,013 (+380.09%)
Mutual labels:  events, keyboard
Hotkeys
➷ A robust Javascript library for capturing keyboard input. It has no dependencies.
Stars: ✭ 5,165 (+2347.87%)
Mutual labels:  hotkeys, keyboard
Sysend.js
Send messages between open pages or tabs in same browser
Stars: ✭ 347 (+64.45%)
Mutual labels:  events, browser
Pyhooked
Pure Python hotkey hook, with thanks to pyHook and pyhk
Stars: ✭ 150 (-28.91%)
Mutual labels:  hotkeys, keyboard
React Native Listener
A utility component to allow easy access to browser native events
Stars: ✭ 136 (-35.55%)
Mutual labels:  events, browser
Noel
A universal, human-centric, replayable javascript event emitter.
Stars: ✭ 158 (-25.12%)
Mutual labels:  events, browser
Filter Console
Filter out unwanted `console.log()` output
Stars: ✭ 203 (-3.79%)
Mutual labels:  browser
Transcrypt
Python 3.7 to JavaScript compiler - Lean, fast, open! -
Stars: ✭ 2,502 (+1085.78%)
Mutual labels:  browser
Client
The nOS Client
Stars: ✭ 202 (-4.27%)
Mutual labels:  browser
Keyboardavoidanceswiftui
How to move SwiftUI view up when keyboard appears https://www.vadimbulavin.com/how-to-move-swiftui-view-when-keyboard-covers-text-field/
Stars: ✭ 198 (-6.16%)
Mutual labels:  keyboard
Goose Parser
Universal scrapping tool, which allows you to extract data using multiple environments
Stars: ✭ 211 (+0%)
Mutual labels:  browser
Browserify
browser-side require() the node.js way
Stars: ✭ 13,929 (+6501.42%)
Mutual labels:  browser

is-hotkey

A simple way to check whether a browser event matches a hotkey.


Features

  • Uses a simple, natural syntax for expressing hotkeys—mod+s, cmd+alt+space, etc.
  • Accepts mod for the classic "cmd on Mac, ctrl on Windows" use case.
  • Can use either event.which (default) or event.key to work regardless of keyboard layout.
  • Can be curried to reduce parsing and increase performance when needed.
  • Is very lightweight, weighing in at < 1kb minified and gzipped.

Example

The most basic usage...

import isHotkey from 'is-hotkey'

function onKeyDown(e) {
  if (isHotkey('mod+s', e)) {
    ...
  }
}

Or, you can curry the hotkey string for better performance, since it is only parsed once...

import isHotkey from 'is-hotkey'

const isSaveHotkey = isHotkey('mod+s')

function onKeyDown(e) {
  if (isSaveHotkey(e)) {
    ...
  }
}

That's it!


Why?

There are tons of hotkey libraries, but they're often coupled to the view layer, or they bind events globally, or all kinds of weird things. You don't really want them to bind the events for you, you can do that yourself.

Instead, you want to just check whether a single event matches a hotkey. And you want to define your hotkeys in the standard-but-non-trivial-to-parse syntax that everyone knows.

But most libraries don't expose their parsing logic. And even for the ones that do expose their hotkey parsing logic, pulling in an entire library just to check a hotkey string is overkill.

So... this is a simple and lightweight hotkey checker!


API

import isHotkey from 'is-hotkey'

isHotkey('mod+s')(event)
isHotkey('mod+s', { byKey: true })(event)

isHotkey('mod+s', event)
isHotkey('mod+s', { byKey: true }, event)

You can either pass hotkey, [options], event in which case the hotkey will be parsed and compared immediately. Or you can passed just hotkey, [options] to receive a curried checking function that you can re-use for multiple events.

isHotkey('mod+a')
isHotkey('Control+S')
isHotkey('cmd+opt+d')
itHotkey('Meta+DownArrow')
itHotkey('cmd+down')

The API is case-insentive, and has all of the conveniences you'd expect—cmd vs. Meta, opt vs. Alt, down vs. DownArrow, etc.

It also accepts mod for the classic "cmd on Mac, ctrl on Windows" use case.

import isHotkey from 'is-hotkey'
import { isCodeHotkey, isKeyHotkey } from 'is-hotkey'

isHotkey('mod+s')(event)
isHotkey('mod+s', { byKey: true })(event)

isCodeHotkey('mod+s', event)
isKeyHotkey('mod+s', event)

By default the hotkey string is checked using event.which. But you can also pass in byKey: true to compare using the KeyboardEvent.key API, which stays the same regardless of keyboard layout.

Or to reduce the noise if you are defining lots of hotkeys, you can use the isCodeHotkey and isKeyHotkey helpers that are exported.

import { toKeyName, toKeyCode } from 'is-hotkey'

toKeyName('cmd') // "meta"
toKeyName('a') // "a"

toKeyCode('shift') // 16
toKeyCode('a') // 65

You can also use the exposed toKeyName and toKeyCode helpers, in case you want to add the same level of convenience to your own APIs.

import { parseHotkey, compareHotkey } from 'is-hotkey'

const hotkey = parseHotkey('mod+s', { byKey: true })
const passes = compareHotkey(hotkey, event)

You can also go even more low-level with the exposed parseHotkey and compareHotkey functions, which are what the default isHotkey export uses under the covers, in case you have more advanced needs.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].