JavaScript keyboard shortcuts for web application [closed]
Asked Answered
G

7

29

I want to develop a web application, which should (ideally) be fully usable via the keyboard. I know how to handle keyboard events in JavaScript, but managing them for a larger application is quite boring.

Is there a library which makes that process easier?

Please note that I'm not interested in a full-blown Web GUI framework. I want to keep control over my webpage/application.

Gonyea answered 29/4, 2011 at 19:38 Comment(3)
Have you seen this: openjs.com/scripts/events/keyboard_shortcutsReifel
bounty has nothing to do with accept; see stackoverflow.com/faq#bountyAckler
Here is an article regarding choosing keyboard shortcuts for Web applications: medium.com/design-ux/a7c3b7b408eePurely
J
37

Check out my project:

https://github.com/oscargodson/jkey

And demos:

http://oscargodson.github.com/jKey/

Feel free to use it and if you want, contribute :)

Jadajadd answered 2/5, 2011 at 22:43 Comment(6)
I thought I had accepted your answer, so I'm wondering why you got only 50% of the bounty!?Gonyea
Its because you accepted after the time was up, its ok, ill take 25 ;) You could also flag this as an error if you know for sure you accepted before the voting time was up.Jadajadd
@oscar accept has nothing to do with bounties. stackoverflow.com/faq#bountyAckler
@Jeff Well, its only half the bounty and i know you only get half the bounty if you wait past the accept time, right? Thats why I assumed it was for that reason. Why would I only get half the bounty then? Just curiousJadajadd
@Oscar It is explained on the page Jeff linked to. The bounty is not automatically awarded to the accepted answer, it has to be manually awarded by clicking the little award button. As explained in the faq, the reason you got half the bounty is because: If you do not award your bounty within 7 days, the highest voted answer created after the bounty started with at least 2 upvotes will be awarded half the bounty amount. (+1 for a useful answer, btw)Sequin
When I press 'a', 'd' I get "you pressed alt + d" ehm... not exactly what I was expecting...Judgeship
C
18

I just developed one of my own called Mousetrap. Check it out.

Carillon answered 10/7, 2012 at 2:56 Comment(4)
Mousetrap was one of those things where it all just worked like I hoped. Nice work.Poeticize
I concur, totally kicks ass! Dropped it in and was away. Implemented keyboard shortcuts within half an hour of downloading it. And I am a js n00b!Canonical
konami code - awesomeMode
(On the Mousetrap page: JavaScript, not Javascript.)Overestimate
B
10

You can use Hotkeys - a plugin for jQuery. jQuery is a quite lightweight JavaScript library - it is a required JavaScript file for using Hotkeys.

Barmecide answered 4/5, 2011 at 19:21 Comment(2)
+1 for mentioning jQuery hotkeys. However, your following sentences don't make a whole lot of sense as written.Obscurantism
Link;s dead - try code.google.com/p/js-hotkeysNeeley
B
5

You could start by reading about the accesskey attribute:

This attribute assigns an access key to an element. An access key is a single character from the document character set. Note. Authors should consider the input method of the expected reader when specifying an accesskey.
[...]
The invocation of access keys depends on the underlying system. For instance, on machines running MS Windows, one generally has to press the "alt" key in addition to the access key. On Apple systems, one generally has to press the "cmd" key in addition to the access key.

You can also put the accesskey attribute on <a> elements, an example of this usage can be found on the "Random Article" sidebar link on Wikipedia.

Basilica answered 29/4, 2011 at 19:43 Comment(3)
Accesskeys are for accessibility, ex: <a href="home.php" accesskey="1">Home</a>.Thebault
@boudou: True but there's no reason to limit accessibility to the mouseless. The <label> element is also for accessibility but that doesn't mean that only people using screen readers are allowed to use it.Basilica
Example: In Firefox, Shift + Alt + U will change the focus to an input element in a form if the input element has the attribute accesskey="U".Overestimate
G
3

I would strongly encourage you to check out Thomas Fuchs' keymaster for doing keyboard shortcuts in web applications: https://github.com/madrobby/keymaster

It makes it quite simple:

// Define short of 'a'
key('a', function(){ alert('you pressed a!') });

// Returning false stops the event and prevents default browser events
key('ctrl+r', function(){ alert('stopped reload!'); return false });

// Multiple shortcuts that do the same thing
key('⌘+r, ctrl+r', function(){ });
Glaucoma answered 30/1, 2013 at 7:58 Comment(1)
This is brilliant. Is there any way to prevent the bypass of the key? I have implemented this so that pressing 'n' opens a dialog with a new item. However, the 'newItem-function also put focus upon the item name, and the 'n' character is bypassed in.Elyssa
T
2

This one is very easy to use.

Example:

shortcut.add("Up",     // Key
             go_up()); // Function
Thebault answered 9/5, 2011 at 10:10 Comment(0)
G
2

You could use the accesskey HTML attribute as it would then make your web application accessible.

Use the KeyTips jQuery Plugin to display them to the user in a similar way to Office Ribbon keyboard shortcuts.

Enter image description here

Try the Demo. Code on GitHub.

Note that the Wikipedia page on accesskey lists the modifier keys to invoke access keys for different browsers.

See also the A List Apart article: Accesskeys: Unlocking Hidden Navigation

Grof answered 13/7, 2012 at 11:18 Comment(1)
Example: In Firefox, Shift + Alt + U will change the focus to an input element in a form if the input element has the attribute accesskey="U".Overestimate

© 2022 - 2024 — McMap. All rights reserved.