jaywcjlove /hotkeys
➷ A robust Javascript library for capturing keyboard input. It has no dependencies.
★ 7,137⑂ 417👁 69⑃ 326● 233▤ 117
More from this author
+1
Daily
+5
Weekly
+10
Monthly
7,137
stars
417
forks
69
watchers
326
prs
233
issues
117
releases
README
Using my app is also a way to support me:
Hotkeys
HotKeys.js is an input capture library with some very special features, it is easy to pick up and use, has a reasonable footprint (~8kB) (gzipped: 3.8kB), and has no dependencies. It should not interfere with any JavaScript libraries or frameworks. Official document demo preview, compatibility test. More examples.
bash
╭┈┈╮ ╭┈┈╮ ╭┈┈╮
┆ ├┈┈..┈┈┈┈┈.┆ └┈╮┆ ├┈┈..┈┈┈┈┈..┈┈.┈┈..┈┈┈┈┈.
┆ ┆┆ □ ┆┆ ┈┤┆
hotkeys('ctrl+a,ctrl+b,r,f', function (event, handler){
switch (handler.key) {
case 'ctrl+a': alert('you pressed ctrl+a!');
break;
case 'ctrl+b': alert('you pressed ctrl+b!');
break;
case 'r': alert('you pressed r!');
break;
case 'f': alert('you pressed f!');
break;
default: alert(event);
}
});
UMD (Universal Module Definition) - For CommonJS/AMD environments:
html
ES Module - For modern browsers with module support:
html
import hotkeys from 'https://unpkg.com/hotkeys-js/dist/hotkeys-js.js';
hotkeys('ctrl+a', function(event, handler){
alert('you pressed ctrl+a!');
});
Used in React
react-hotkeys is the React component that listen to keydown and keyup keyboard events, defining and dispatching keyboard shortcuts. Detailed use method please see its documentation react-hotkeys.
react-hotkeys-hook - React hook for using keyboard shortcuts in components. Make sure that you have at least version 16.8 of react and react-dom installed, or otherwise hooks won't work for you.
Browser Support
Hotkeys.js has been tested and should work in.
shell
Internet Explorer 6+
Safari
Firefox
Chrome
Supported Keys
HotKeys understands the following modifiers: ⇧, shift, option, ⌥, alt, ctrl, control, command, and ⌘.
The following special keys can be used for shortcuts: backspace, tab, clear, enter, return, esc, escape, space, up, down, left, right, home, end, pageup, pagedown, del, delete, f1 through f19, num_0 through num_9, num_multiply, num_add, num_enter, num_subtract, num_decimal, num_divide.
⌘ Command()
⌃ Control
⌥ Option(alt)
⇧ Shift
⇪ Caps Lock(Capital)
~~fn Does not support fn~~
↩︎ return/Enter space
Defining Shortcuts
One global method is exposed, key which defines shortcuts when called directly.
ts
declare interface HotkeysInterface extends HotkeysAPI {
(key: string, method: KeyHandler): void;
(key: string, scope: string, method: KeyHandler): void;
(key: string, option: HotkeysOptions, method: KeyHandler): void;
shift?: boolean;
ctrl?: boolean;
alt?: boolean;
option?: boolean;
control?: boolean;
cmd?: boolean;
command?: boolean;
}
declare interface HotkeysAPI {
setScope: SetScope;
getScope: GetScope;
deleteScope: DeleteScope;
getPressedKeyCodes: GetPressedKeyCodes;
getPressedKeyString: GetPressedKeyString;
getAllKeyCodes: GetAllKeyCodes;
isPressed: IsPreTop issues
#26Whether support Sequence of keys?16 comments · 2018-05-13#92Right command does not fires on macOS15 comments · 2019-06-24#434the callback is not executed when scope is' all '12 comments · 2023-07-04#90Make hotkeys events removable12 comments · 2019-06-14#126Code lifted from Keymaster without attribution10 comments · 2020-02-07#426binding detection does not work8 comments · 2023-05-06#244command+c+k问题探讨8 comments · 2021-01-26#65Not working after hitting Tab8 comments · 2019-04-13
Latest release
v4.1.2 v4.1.2 2026-11-06Documentation v4.1.2: https://raw.githack.com/jaywcjlove/hotkeys/8bfc8b4/index.html Comparing Changes: https://github.com/jaywcjlove/hotkeys-js/compare/v4.1.1...v4.1.2 bash npm i [email protected] - 🐞 fix: reset modifier state on window focus changes ref:0 (558) 28b0824 @blueDaydreaming725