Robust keyboard shortcut handling using JavaScript
Asked Answered
C

6

6

What's the most robust way of creating a global keyboard shortcut handler for a Web application using JavaScript i.e. which event(s) should I handle and what should the event handler(s) be attached to?

I want something like the system in Gmail which can handle both single keypress shortcuts and also shortcuts with modifier keys e.g. Ctrl + B etc. The code has to work in IE 6 as well as modern browsers.

I have the Prototype framework available to use but not jQuery, so please, no jQuery-specific answers!

Chlamydospore answered 4/3, 2009 at 16:24 Comment(1)
openjs.com/scripts/events/keyboard_shortcutsTranslator
E
5

The HotKey library available in the LivePipe controls package works with Prototype and is IE compatible.

http://livepipe.net/extra/hotkey

Exorcist answered 4/3, 2009 at 18:5 Comment(3)
I'm going to accept your answer because this looks like exactly what I'm looking for and because there's lots of other interesting stuff on that site, so thanks!Chlamydospore
Be warned, HotKey apparently accepts combinations if unspecified modifiers are pressed (eg. specified ctrl + a, pressed ctrl + shift + a, the ctrl + a is detected).Weakwilled
It would also be nice if it detected OS X and replaced ctrl with cmd/meta.Weakwilled
S
6

Just thought I'd throw another into the mix. I recently released a library called Mousetrap. Check it out at http://craig.is/killing/mice

Steam answered 11/7, 2012 at 16:18 Comment(0)
E
5

The HotKey library available in the LivePipe controls package works with Prototype and is IE compatible.

http://livepipe.net/extra/hotkey

Exorcist answered 4/3, 2009 at 18:5 Comment(3)
I'm going to accept your answer because this looks like exactly what I'm looking for and because there's lots of other interesting stuff on that site, so thanks!Chlamydospore
Be warned, HotKey apparently accepts combinations if unspecified modifiers are pressed (eg. specified ctrl + a, pressed ctrl + shift + a, the ctrl + a is detected).Weakwilled
It would also be nice if it detected OS X and replaced ctrl with cmd/meta.Weakwilled
W
4

JimmyP posted this as a comment, but it deserves to be an answer for voting purposes.

http://www.openjs.com/scripts/events/keyboard_shortcuts/

Weakwilled answered 4/3, 2009 at 16:24 Comment(2)
I know, that's why I asked him why he hadn't posted it as an answer. Upvoted.Chlamydospore
I know, that's why I posted it as an answer. ;) And I posted it community wiki because it's not my points to earn.Weakwilled
R
3

What I would do is attach onKeyUp events to the document.body. Then, in this event handler, I would use the Element.fire method to fire a custom event. Though this step is optional, it will help in decoupling the event handler from the action to be performed, and you can use the same custom-event handler from say an button click event.

$(document.body).observe("keyup", function() {
    if(/* key + modifier match */) {
        $(document.body).fire("myapp:mycoolevent");
    }
});

$(document.body).observe("myapp:mycoolevent", function() {
    // Handle event.
});

Later, to bind the same event to a button click:

$(button).observe("click", function() {
    $(document.body).fire("myapp:mycoolevent");
});

As far as handling modifier keys is concerned, check out this resource (very old, but still looks applicable) for more help.

Ruthful answered 4/3, 2009 at 17:30 Comment(1)
Keyup seems problematic. At least on OS X (but I believe it's true on Windows and most Linux WMs as well), most key commands activate on keydown. This expectation (with proper checking) also enforces certain expectations on shortcuts: modifier keys must be first, only one printable key per shortcut.Weakwilled
W
2

There is also new JavaScript library called jwerty, it's easy to use and doesn't rely on jQuery.

Windpipe answered 18/10, 2011 at 10:29 Comment(0)
S
0

I recommend having a look at Keystrokes. It makes this sort of thing extremely easy.

import { bindKey, bindKeyCombo } from '@rwh/keystrokes'

bindKey('a', () =>
  console.log('You\'re pressing "a"'))

bindKeyCombo('ctrl > y, r', () =>
  console.log('You pressed "ctrl" then "y", released both, and are pressing "r"'))
Sociality answered 7/12, 2022 at 0:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.