这是最好的Javascript键盘事件库.(热键,快捷键)

zjm*_*126 29 javascript javascript-events

我想创建一个使用很多键盘事件的bbs论坛,

那么哪个是最好的键盘事件库,

谢谢

Cra*_*aig 33

只是另一个投入混合.我最近发布了一个名为Mousetrap的产品.您可以在http://craig.is/killing/mice上查看示例

  • 它使用Apache 2.0许可证. (2认同)

Rob*_*rst 12

试试KeyboardJS

就像它一样简单

KeyboardJS.on('a', function(){ alert('hello!'); });
Run Code Online (Sandbox Code Playgroud)

但同样灵活

var bindInstance = KeyboardJS.on('ctrl + a, ctrl + b, c', function(event, keysPressedArray, keyComboString){

    //you get the event object
    console.log('event object', event);

    //you get the keys pressed array
    console.log('keys pressed', keysPressedArray);

    //you get the key combo string
    console.log('combo pressed', keyComboString);

    console.log('I will fire when \'ctrl + a\' or \'ctrl +b\' or \'c\' is pressed down');

    //block event bubble
    return false;

}, function(event, keysPressedArray, keyComboString) {

    console.log('I will fire on key up');

    //block event bubble
    return false;

});
Run Code Online (Sandbox Code Playgroud)

你可以通过调用来清除绑定

bindInstance.clear();
Run Code Online (Sandbox Code Playgroud)

您可以使用特定键清除所有绑定

KeyboardJS.clear('a, b');
Run Code Online (Sandbox Code Playgroud)

它的开源,可在Github上获得.它在Ether中是一个全局库或用于RequireJS的AMD模块.

这是一个介绍视频.

在那里,现在不再担心键盘和代码你的应用程序.;)


Dan*_*ite 1

这个对于 jquery 来说相当不错。

https://github.com/jeresig/jquery.hotkeys