⌨ Awesome handling of keyboard events

Overview

No Longer Actively Maintained

If someone would like to take over maintainence, feel free to get in touch (@keithamus on twitter). I'll happily transfer this over.

jwerty

Awesome handling of keyboard events
http://keithamus.github.io/jwerty/

Gitter

NPM Downloads Release Gittip donate button

Sponsor

jwerty is a JS lib which allows you to bind, fire and assert key combination strings against elements and events. It normalises the poor std api into something easy to use and clear.

jwerty is a small library, weighing in at around 1.5kb bytes minified and gzipped (~3kb minified). jwerty has no dependencies, but is compatible with jQuery, Zepto, Ender or CanJS if you include those packages alongside it. You can install jwerty via npm (for use with Ender) or Bower.

For detailed docs, please read the README-DETAILED.md file.

The Short version

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');
Comments
  • Unbinding should be internalized

    Unbinding should be internalized

    Referring to issue https://github.com/keithamus/jwerty/issues/19, returning an unbindable object creates enormous complexity in the client. We now have to internalize jwerty subscriptions in, say, an array or a dictionary.

    The better approach would simply be this:

    jwerty.unbind('tab', _context);
    

    Internally, jwerty would handle the unbind.

    I have written three keyboard navigators with the following class hierarchy:

    KeyboardNavigator

    KeyboardMatrixNavigator

    KeyboardCalendarNavigator

    In the matrix navigator, I simply wish to remap up and down to #prevRow and #nextRow, respectively. But I can't really do that without a whole lot of management machinery of the jwerty subscriptions.

    In the #bindKeys method of KeyboardMatrixNavigator, where I override the #bindKeys method in KeyboardNavigator, I should simply be able to do this:

    KeyboardNavigator.prototype.bindKeys.call(this);
    jwerty.unbind('up', _context);
    jwerty.key('up', ...)
    jwerty.unbind('down', _context);
    jwerty.key('down', ...)
    

    Your thoughts?

    opened by estaylorco 6
  • Add components.json with jquery-jwerty for bower

    Add components.json with jquery-jwerty for bower

    Add support for the package manager http://twitter.github.io/bower/ it's like the standard for web components, It will be great and will give visibility to this project.

    opened by luishdez 6
  • Don't override real global object

    Don't override real global object

    Example of problem:

    For code:

    $ = global.$ = global.jQuery = require('jquery')
    jwerty = require('jwerty').jwerty
    
    // ...
    
    jwerty.fire('esc', $el)
    

    I've got: "TypeError: Expecting a function in instanceof check, but got [object Object]".

    When I put console output to this code:

    (function (global, exports) {
    
        console.log('#######')
        console.log('Global:')
        console.log(global)
        console.log('jQuery:')
        console.log(global.jQuery)
        console.log('#######')
    
        // Helper methods & vars:
        var $d = global.document
        ,   $ = (global.jQuery || global.Zepto || global.ender || $d)
        ,   $$
        ,   $b
        ,   ke = 'keydown';
    
        // ...
    

    Output:

    #######
    Global:
    {}
    jQuery:
    undefined
    #######
    

    This happen because this != global in require:

    $ node
    > this === global
    true
    

    but

    $ cat example.js                                                                                                                                                                                console.log(this === global)
    $ node
    > require('./example')
    false
    
    opened by kossnocorp 6
  • Browserify + node-webkit compatiblity

    Browserify + node-webkit compatiblity

    In browserify environment, jQuery/Zepto/Ender is not available in the context of the module. It need to be required. In node-webkit environment, the context (or global in the closure) is an empty object. To get the expected window object, global.window is provided.

    help wanted 
    opened by LoicMahieu 5
  • document using + sign

    document using + sign

    is it possible to use + as the trigger key or not because it's a special glue character for jwerty commands?

    Trying to do something like this ctrl+shift++

    docs help wanted 
    opened by btopro 4
  • Disabling jwerty from focused textarea, input, select, etc

    Disabling jwerty from focused textarea, input, select, etc

    Hello!

    Looking through your extended readme it looks like this should be possible but I'm having a hard time tracking down what I might be doing wrong

    <script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js'></script>
    <script type='text/javascript' src='../lib/jwerty.js'></script>
    
    <input id='q' name='q' />
    
    <script>
        focus_input = function(){
          jQuery('#q').focus();
          return true;
        }
    
        alert_j = function(){
          alert('j');
          return true;
        }
    
        jwerty.key('i', focus_input);
        jwerty.key('j', alert_j);
        jwerty.key('j', false, '#q');
    </script>
    

    In this case I bind "i" to focus on that form input and "j" to alert the letter "j".

    I expect that when the element is focused on #q, however, that the "j" key event will not fire as specified in the last line within the script tag.

    Desired effect:

    • hit i key => focus on #q form input
    • hit j key => "j" shows up in input but no alert is triggered

    Current (undesired) effect:

    • hit i key => focus on #q form input
    • hit j key => "j" shows up in input and alert is triggered

    Looking forward to your thoughts!

    Using the latest jwerty and jQuery 1.10.2

    question 
    opened by jayroh 3
  • Bugs with some symbols (OSX)

    Bugs with some symbols (OSX)

    There is a bug with detecting some symbols such as parentheses and hyphens. For example i've got "½" instead of "-" or "9" and "0" instead of "(" and ")". But if with parentheses i can understand the logic (shift + 0 is opening bracket), then the logic of "½" is very confusing.

    wontfix 
    opened by nicholasrq 2
  • Keyboard shortcut to launch modal window...

    Keyboard shortcut to launch modal window...

    0 down vote favorite

    I am uing your link for integrating keyboard short cuts.. It redirects to the given URL after pressing the key..

    jwerty.key('f2', function () { window.location = document.getElementById('SaleLink').href; });

    I am using following code to launch the bootstrap modal window :

    [a href=PopUp.php?Id=1 data-toggle="modal" data-target="#myModal">Open</a]

    $('#myModal').modal({ show: false});
    $('#myModal').on('hidden', function () {
        console.log('modal is closed');
    })
    $("a[data-toggle=modal]").click(function (e) {
    lv_target = $(this).attr('data-target');
    lv_url = $(this).attr('href');
    $(lv_target).load(lv_url);
    }); 
    

    It works fine when i click on the link...i am trying to add this code to launch the modal window when i press a key say f2 ...since it redirects directly to the given URL therefore the modal window is not opening....Can you please suggest any solution for this??

    question 
    opened by webmastervinay 2
  • There is not an unbind function

    There is not an unbind function

    Hi, i'm using this pluggin, it's really good. I need to create and remove dynamic shortcuts, so i need an unbind function. I think it's a good idea to extend jwerty with this functionality. I have implemented a jwert.unbind function that unbinds all the events related to a key code. It works fine, but just for jquery.

    If some want i can share the code. And if anyone has done a similar function I'd like to see it to compare with my code.

    Thanks!

    enhancement 
    opened by nicoburghi 2
  • Numbers binding

    Numbers binding

    Hi,

    I'm evaluating your library for keyboard navigation in my project. I really like the concept but I have some difficulties with number keys.

    I want to bind numbers, from 0 to 9, so I started with this :

    jwerty.key('[0-9]', function (e) {
        alert(e.which);
    }); 
    

    It didn't work for me because I have an azerty keyboard and I obtain '0' by pressing 'shift+0' (http://www.computerhope.com/help/azerty.gif). This leads to :

    jwerty.key('shift+[0-9]', function (e) {
        alert(e.which);
    }); 
    

    Works great, but I don't care if the numbers come from the numpad or the left of the keyboard. I have to handle all of them the same way. So I tried this :

    // does not work
    jwerty.key('shift+[0-9],num-[0-9]', function (e) {
        alert(e.which);
    });
    
    // does not work
    jwerty.key('shift+[0-9],num-0,num-1,num-2,num-3,...', function (e) {
        alert(e.which);
    })
    

    and finally this :

    jwerty.key('shift+[0-9]', function (e) {
        alert(e.which);
    });
    jwerty.key('num-0', function (e) {
        alert(e.which);
    });
    jwerty.key('num-1', function (e) {
        alert(e.which);
    });
    .
    .
    .
    jwerty.key('num-9', function (e) {
        alert(e.which);
    });
    

    The last code works, but is not as easy as promising :)

    I think the more inuitive way of handling numbers would be the first solution : '[0-9]'. In my case, I don't need the distinction between numpad and normal keyboard (do some people really need that?).

    What do you think?

    Best regards Olivier

    question 
    opened by olecrivain 2
  • fire f11

    fire f11

    Hi,

    I am trying to get a full screen mode. i.e firing f11 on body load.

    I am using the below code

    jwerty.fire('{122}');

    //Below is the full code hope this will help you to run it..

    I look forward to hear from you.

    Thanks & Regards Ganesh K

    invalid 
    opened by krigans 1
  • Unbind doesn't work.

    Unbind doesn't work.

    I suddenly discovered that unbind functionality doesn't work.

    My code looks like that:

    var k = jwerty.key('↑/↓/←/→/pgdown/pgup/home/end', callback);
    ...
    k.unbind(); // actually, k is undefined because jwerty.key returns nothing
    

    I tried to google the issue, but nobody complains...

    opened by evpozdniakov 2
  • Difference between source code and archives/npmjs versions

    Difference between source code and archives/npmjs versions

    Hey ! I encounter an issue with your (great !) library. When i install jwerty using npm, it download me a file with :

    (function (global, exports) {
    ...
    }(this, ...));
    

    This doesn't work with my project, because the global isn't set to window But when i'm looking to your source code, i see :

    (function (global, exports) {
    ...
    }(typeof global !== 'undefined' && global.window || this, ...));
    

    which works well. Can you explain me why there is a difference between these two (same ?) files ? Thank you in advance =)

    opened by thfontaine 2
  • sends key history to triggered function

    sends key history to triggered function

    Tracks key press history and adds a keySequence object to the final event (which is already sent to triggered function). Useful when using multiple-choice or regex-like syntax. i.e. get the actual 3-digit code that triggered shift+;,[0-9],[0-9],[0-9]. keySequence is an object that contains an object per key press, and also a string representation called sequence using pipes to separate keys: shift+h|e|l|l|o|space|ctrl+w|o|r|l|d

    example usage:

    jwerty.key('ctrl+shift+;,[a-e],[f-j],[k-o]' , function() { 
      alert(this.event.keySequence.sequence); 
    });
    

    jwerty has tremendous potential because of regex support, which other libraries do not have. However its overall support for sequences is spotty, for example, (on Chrome 51/Windows at least) sequences which end in a modifier like a,b,shift+c do not trigger. Some regex edge cases are also unsupported: [a-z],[a-z],[a-z],enter will not be triggered by abcd-enter (I would expect bcd-enter to be the match). Still, the support it does have is great and along with ajax requests, allows for simple password-type sequences that can't be revealed with View Source. I really hope you'll revisit this library and finish 0.4. Thanks.

    opened by 2x2xplz 1
  • get actual keys pressed when matching regex or optional

    get actual keys pressed when matching regex or optional

    If I use a regex sequence or optional keys, like your example jwerty.key('ctrl+[0-9],f/h'), is there any way to see which keys were actually pressed? i.e. in this example, which number, and f or h? Thank you.

    opened by 2x2xplz 1
  • jwerty.key(...) returns nothing

    jwerty.key(...) returns nothing

    To reproduce the issue install jwerty with bower. bowert install jwerty

    ~~You can use -F key (force last version) to solve the issue. bowert install jwerty -F~~

    I can solve the issue if I download zip and install from local folder. But then I have a warning about critical dependencies, as I use webpack.

    opened by evpozdniakov 0
Releases(v0.3.2)
Owner
Keith Cirkel
Works on JavaScript at @github. Maintainer of @chaijs.
Keith Cirkel
jQuery Hotkeys lets you watch for keyboard events anywhere in your code supporting almost any key combination.

jQuery.Hotkeys #About jQuery Hotkeys is a plug-in that lets you easily add and remove handlers for keyboard events anywhere in your code supporting al

John Resig 2.6k Jan 2, 2023
A simple micro-library for defining and dispatching keyboard shortcuts. It has no dependencies.

keymaster.js Keymaster is a simple micro-library for defining and dispatching keyboard shortcuts in web applications. It has no dependencies. It’s a w

Thomas Fuchs 6.5k Jan 4, 2023
A keyboard input capturing utility in which any key can be a modifier key.

Keypress Version 2.1.5 Keypress is a robust keyboard input capturing Javascript utility focused on input for games. For details and documentation, ple

David Mauro 3.2k Dec 28, 2022
A JavaScript library for binding keyboard combos without the pain of key codes and key combo conflicts.

KeyboardJS KeyboardJS is a library for use in the browser (node.js compatible). It Allows developers to easily setup key bindings. Use key combos to s

Robert Hurst 2k Dec 30, 2022
Simple library for handling keyboard shortcuts in Javascript

Mousetrap Mousetrap is a simple library for handling keyboard shortcuts in Javascript. It is licensed under the Apache 2.0 license. It is around 2kb m

Craig Campbell 11.3k Jan 3, 2023
A simple library for handling keyboard shortcuts with Alpine.js.

✨ Help support the maintenance of this package by sponsoring me. Alpine.js Mousetrap A simple library for handling keyboard shortcuts with Alpine.js.

Dan Harrin 102 Nov 14, 2022
Remote Keyboard Tutoring System is a web-based system that can be attached to any keyboard synthesizer through a MIDI connector.

The Remote Keyboard Tutoring System is a web-based system that can be attached to any (electronic) keyboard synthesizer through a MIDI connector. Once our system is connected to the keyboard, the user can interactively learn, play or teach in combination with the web application that we provide.

Department of Computer Engineering, University of Peradeniya 3 Nov 15, 2022
A quick-start template for Discord.js v14 in TypeScript that contains handling for commands, events, and interactions!

Typescript-Discord.js-v14-Template A quick-start template for Discord.js v14 in Typescript that contains handling for commands, events, and interactio

Delta 23 Dec 28, 2022
jQuery Hotkeys lets you watch for keyboard events anywhere in your code supporting almost any key combination.

jQuery.Hotkeys #About jQuery Hotkeys is a plug-in that lets you easily add and remove handlers for keyboard events anywhere in your code supporting al

John Resig 2.6k Jan 2, 2023
This project is an Awesome Books web page where users can add or remove books from the site. It also displays a list of the books added to the collection. This Awesome books webpage was created using ES6.

Awesome books App using Module Awesome books App using Module This book list was built using modules and other ES6 syntax. It allows users to add/remo

Esther Udondian 6 Jul 25, 2022
An Awesome Toggle Menu created with HTML,CSS,JQuery,font-awesome and line by line comment.

Demo : https://blackx-732.github.io/AwesomeMenu/ Under open source license No ©copyright issues Anyone can be modify this code as well Specifically we

BlackX-Lolipop 2 Feb 9, 2021
A simple, lightweight JavaScript API for handling browser cookies

JavaScript Cookie A simple, lightweight JavaScript API for handling cookies Works in all browsers Accepts any character Heavily tested No dependency S

null 20.2k Jan 3, 2023
Premium Queue package for handling distributed jobs and messages in NodeJS.

The fastest, most reliable, Redis-based queue for Node. Carefully written for rock solid stability and atomicity. Sponsors · Features · UIs · Install

null 13.5k Dec 31, 2022
Flow control and error handling for Node.js

NOTE: This project is deprecated and no longer being actively developed or maintained. See Issue #50 for details. StrongLoop zone library Overview The

StrongLoop and IBM API Connect 280 Feb 18, 2022
Handling iterables like lazy arrays.

Iterum iterum library provides a class for handling iterable transformations inspired in Array methods and lodash/fp functions. This library also supp

Xavier Garcia Buils 28 Nov 5, 2021
An extension to Async adding better handling of mixed Series / Parallel tasks via object chaining

async-chainable Flow control for NodeJS applications. This builds on the foundations of the Async library while adding better handling of mixed Series

Matt Carter 25 Jul 15, 2019
Simple config handling for your app or module

conf Simple config handling for your app or module All you have to care about is what to persist. This module will handle all the dull details like wh

Sindre Sorhus 1k Jan 7, 2023
Lightweight WebSocket lib with socket.io-like event handling, requests, and channels

ws-wrapper Lightweight and isomorphic Web Socket lib with socket.io-like event handling, Promise-based requests, and channels. What? Much like Socket.

Blake Miner 70 Dec 23, 2022
A set of APIs for handling HTTP and HTTPS requests with Deno 🐿️ 🦕

oak commons A set of APIs that are common to HTTP/HTTPS servers. HTTP Methods (/method.ts) A set of APIs for dealing with HTTP methods. Content Negoti

oak 7 May 23, 2022
A command line interface for file handling using JCore.FileSystem

JCore.FileSystem.Cli Table of Contents JCore.FileSystem.Cli Table of Contents Introduction Installation Uninstall Usage References Articles Packages T

Sniper Code 1 Jan 21, 2022