What is hotkeys-js?
The hotkeys-js npm package is a simple library for handling keyboard shortcuts in web applications. It allows developers to define and manage keyboard shortcuts with ease, providing a straightforward API for binding and unbinding keys.
What are hotkeys-js's main functionalities?
Binding a single key
This feature allows you to bind a single key to a function. In this example, pressing the 'A' key will trigger a console log message.
hotkeys('a', function(event, handler) {
console.log('You pressed A!');
});
Binding multiple keys
This feature allows you to bind multiple keys to a single function. In this example, pressing either 'Ctrl+A' or 'Ctrl+B' will trigger the same console log message.
hotkeys('ctrl+a, ctrl+b', function(event, handler) {
console.log('You pressed either Ctrl+A or Ctrl+B!');
});
Binding key combinations
This feature allows you to bind key combinations to a function. In this example, pressing 'Ctrl+Shift+K' will trigger a console log message.
hotkeys('ctrl+shift+k', function(event, handler) {
console.log('You pressed Ctrl+Shift+K!');
});
Unbinding keys
This feature allows you to unbind a previously bound key. In this example, the 'A' key will no longer trigger any function.
hotkeys.unbind('a');
Filtering key events
This feature allows you to filter which key events should be processed by hotkeys-js. In this example, all key events will be processed.
hotkeys.filter = function(event) {
return true; // Process all key events
};
Other packages similar to hotkeys-js
mousetrap
Mousetrap is a simple library for handling keyboard shortcuts in Javascript. It is similar to hotkeys-js in that it allows you to bind and unbind keys and key combinations. However, Mousetrap has a slightly different API and includes additional features such as sequence bindings and key event handling in specific DOM elements.
keymaster
Keymaster is a simple micro-library for defining and dispatching keyboard shortcuts. It is similar to hotkeys-js in its simplicity and ease of use. Keymaster focuses on providing a minimalistic API for binding keys and key combinations, but it does not include some of the more advanced features found in hotkeys-js, such as filtering key events.
keyboardjs
KeyboardJS is a robust library for handling keyboard input. It offers more advanced features compared to hotkeys-js, such as support for key sequences, key states, and context-specific bindings. While it is more feature-rich, it also has a steeper learning curve compared to the simplicity of hotkeys-js.
设置快捷键

这又是在重复造轮子,呵呵~!!
下载
bower
Run bower info hotkeysjs
to list the available versions.
安装 bower install hotkeysjs
定义快捷键
hotkeys('a', function(){ alert('你按下了 a!') });
hotkeys('ctrl+r', function(){ alert('停止刷新!'); return false });
hotkeys('⌘+r, ctrl+r', function(){ });
支持的键
⇧
, shift
, option
, ⌥
, alt
, ctrl
, control
, command
, ⌘
。
⌘
Command()
⌃
Control
⌥
Option(alt)
⇧
Shift
⇪
Caps Lock(大写)
fn
功能键就是fn
↩︎
return/Enter
修时键判断
可以对下面的修饰键判断 shift
alt
option
ctrl
control
command
if(hotkeys.shift) console.log('大哥你摁下了shift键!');
切换快捷键
hotkeys('o, enter', 'issues', function(){ });
hotkeys('o, enter', 'files', function(){ });
hotkeys.setScope('issues');
兼容模式
var k = hotkeys.noConflict();
k('a', function() {
console.log("这里可以干一些事儿")
});
hotkeys()