处理键盘快捷键的JavaScript库:Combokeys
jopen
10年前
Combokeys是一个在浏览器中处理键盘快捷键的JavaScript库。
整个库大约只有 1.9kb ,没有其它依赖,经测试支持以下浏览器:
- Internet Explorer 6+ (test suite works in IE9+)
- Safari
- Firefox
- Chrome
它已经支持 keypress
, keydown
, 和keyup
事件在特定键,组合键或按键顺序。
// single keys combokeys.bind('4', function() { console.log('4'); }); firstCombokeys.bind("?", function() { console.log('show shortcuts!'); }); secondCombokeys.bind('esc', function() { console.log('escape'); }, 'keyup'); // combinations combokeys.bind('command+shift+k', function() { console.log('command shift k'); }); // map multiple combinations to the same callback combokeys.bind(['command+k', 'ctrl+k'], function() { console.log('command k or control k'); // return false to prevent default browser behavior // and stop event from bubbling return false; }); // gmail style sequences Combokeys.bind('g i', function() { console.log('go to inbox'); }); Combokeys.bind('* a', function() { console.log('select all'); }); // konami code! Combokeys.bind('up up down down left right left right b a enter', function() { console.log('konami code'); });