jwerty是一个JS类库帮助你针对元素和事件来绑定,触发和声明输入键组合。它将难于使用的标准api生成更加清晰并且容易使用的格式。所有的 jwerty事件都要求jwertycode。jwertycode可以以字符串或者数组传递,使用字符串可能是最简单的方式来生成组合。这个类库非常 小,压缩后1.5kb,gzipped大概3kb。不依赖任何类库。兼容jQuery,zepto或者Ender。
Use jwerty.key to bind your callback to a key combo (global shortcuts)
jwerty.key('ctrl+shift+P', function () { [...] }); jwerty.key('⌃+⇧+P', function () { [...] }); Specify optional keys: jwerty.key('⌃+⇧+P/⌘+⇧+P', function () { [...] }); or key sequences: jwerty.key('↑,↑,↓,↓,←,→,←,→,B,A,↩', function () { [...] }); You can also (since 0.3) specify regex-like ranges: jwerty.key('ctrl+[a-c]', function () { [...] }); // fires for ctrl+a,ctrl+b or ctrl+c Pass in a context to bind your callback: jwerty.key('⌃+⇧+P/⌘+⇧+P', function () { [...] }, this); Pass in a selector to bind a shortcut local to that element: jwerty.key('⌃+⇧+P/⌘+⇧+P', function () { [...] }, this, '#myinput'); Pass in a selector's context, similar to jQuery's $('selector', 'scope'): jwerty.key('⌃+⇧+P/⌘+⇧+P', function () { [...] }, this, 'input.email', '#myForm'); If you're binding to a selector and don't need the context, you can ommit it: jwerty.key('⌃+⇧+P/⌘+⇧+P', function () { [...] }, 'input.email', '#myForm'); Calls to jwerty.key return a subscription handle that you can use to disconnect the callback var h = jwerty.key('ctrl+shift+P', function () { [...] }) h.unbind() Use jwerty.event as a decorator, to bind events your own way: $('#myinput').bind('keydown', jwerty.event('⌃+⇧+P/⌘+⇧+P', function () { [...] })); Use jwerty.is to check a keyCombo against a keyboard event: function (event) { if ( jwerty.is('⌃+⇧+P', event) ) { [...] } } Or use jwerty.fire to send keyboard events to other places: jwerty.fire('enter', 'input:first-child', '#myForm');
近期评论