最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

prototypejs - Robust keyboard shortcut handling using JavaScript - Stack Overflow

programmeradmin2浏览0评论

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!

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!

Share Improve this question edited Dec 28, 2011 at 11:45 Rob W 349k87 gold badges807 silver badges682 bronze badges asked Mar 4, 2009 at 16:24 John TopleyJohn Topley 115k47 gold badges199 silver badges240 bronze badges 1
  • openjs.com/scripts/events/keyboard_shortcuts – James Commented Mar 4, 2009 at 16:51
Add a comment  | 

6 Answers 6

Reset to default 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

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

http://livepipe.net/extra/hotkey

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

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

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.

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

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"'))
发布评论

评论列表(0)

  1. 暂无评论