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
npm
下载 npm install hotkeys-js
定义快捷键
hotkeys('a', function(event){
if(event.target === "input"){
alert('你在输入框中按下了 a!')
}
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('ctrl+o, ctrl+alt+enter', 'issues', function(){
console.log('干点活儿');
});
hotkeys('o, enter', 'files', function(){
console.log('另一种活儿');
});
hotkeys.setScope('issues');
解除绑定
hotkeys.unbind("ctrl+o, ctrl+alt+enter")
解除绑定两组快捷键
hotkeys.unbind("ctrl+o","files")
解除绑定名字叫files钟的一组快捷键
键判断
判断摁下的键是否为某个键
hotkeys('a', function(){
console.log(hotkeys.isPressed("A"));
console.log(hotkeys.isPressed(65));
});
获取摁下键值
获取摁下绑定键的键值
hotkeys('command+ctrl+shift+a,f', function(){
console.log(hotkeys.getPressedKeyCodes());
})
过滤
INPUT
SELECT
TEXTAREA
默认不处理。
key.filter
返回 true
快捷键设置才会起作用,flase
快捷键设置失效。
key.filter = function(event){
return true;
}
兼容模式
var k = hotkeys.noConflict();
k('a', function() {
console.log("这里可以干一些事儿")
});
hotkeys()