If you want to enhance your web app, Javascript keyboards shortcuts is definitely something to consider. In this article, you'll learn to use JS keyboard shortcuts, with and without the JQuery framework.

Keyboard shorcuts on the web, let’s talk about it

The most important thing to think about when implementing Javascript keyboard shortcuts on a web app is to avoid redefining shortcuts provided by the client’s browser. Imagine that you’re using a web app, you want to close the tab by pressing Cmd+W, and the web app trigger an action, just because the developer redefined a keyboard shortcut used by your browser…How annoying. This is why I recommend to avoid using the Ctrl (Windows) or Cmd (Mac) key for your web app shortcuts. You should definitely use another key as such as F1, for example.

As you may already know, Javascript have lots of useful event listeners. For keyboard shortcuts, we’ll use onkeyup,
which allow you to perform an action when a key is pressed. Then, we’ll just have to compare the returned value with the keyboard code associated with the key used by one of our custom defined keyboard shortcuts.

Keyboard codes are simple code consisting of 2 or 3 numbers. Each keyboard key have its own keyboard code. For exemple, the Ctrl key code is 17.

For a full reference of keyboard keys, scroll down to the end of this post.

Examples

In the following example, we’re simply going to verify the key pressed down by the user. If the key pressed are Ctrl+S, a function will be triggered.

First example, without JQuery:

var isCtrl = false;
document.onkeyup=function(e) {
    if(e.which == 17) isCtrl=false;
}document.onkeydown=function(e){
    if(e.which == 17) isCtrl=true;
    if(e.which == 83 && isCtrl == true) {
         alert('Keyboard shortcuts are cool!');
         return false;
    }
}

Example with the JQuery framework:

var isCtrl = false;$(document).keyup(function (e) {
if(e.which == 17) isCtrl=false;
}).keydown(function (e) {
    if(e.which == 17) isCtrl=true;
    if(e.which == 83 && isCtrl == true) {
        alert('Keyboard shortcuts + JQuery are even more cool!');
 	return false;
 }
});

In theses examples, we started by verifying that the Ctrl is pressed by the user. If yes, the initial value of the isCtrl variable is set to true. If the keys are released, isCtrl will be set to false again.

Once done, we have to verify that the second pressed key is S. As the shortcut consists of a keyboard combination, we also have to check that is isCtrl variable have true as a value.

If both the isCtrl variable ius set to true and the second pressed key is S, then we can trigger a Javascript alert to display a message. Of course, in a “real” web app, you’ll use a more useful function ;)

Keyboard codes reference

KeyKeyboard code
Backspace8
Tab9
Enter13
Shift16
Ctrl17
Alt18
Pause19
Capslock20
Esc27
Page up33
Page down34
End35
Home36
Left arrow37
Up arrow38
Right arrow39
Down arrow40
Insert45
Delete46
048
149
250
351
452
553
654
755
856
957
a65
b66
c67
d68
e69
f70
g71
h72
i73
j74
k75
l76
m77
n78
o79
p80
q81
r82
s83
t84
u85
v86
w87
x88
y89
z90
0 (numpad)96
1 (numpad)97
2 (numpad)98
3 (numpad)99
4 (numpad)100
5 (numpad)101
6 (numpad)102
7 (numpad)103
8 (numpad)104
9 (numpad)105
*106
+107
-109
.110
/111
F1112
F2113
F3114
F4115
F5116
F6117
F7118
F8119
F9120
F10121
F11122
F12123
=187
Coma188
Slash /191
Backslash \220
 

26 Comments

  1. Posted June 9, 2009 at 10:01 pm | Permalink

    Implementing and using keyboard shortcuts can really enhance usability and accessibility of your site. But the question is do you have such a complex site and need for it.

  2. Posted June 10, 2009 at 3:02 am | Permalink

    That’s pretty nifty. But wouldn’t it be a little bit hard at first for the users like me?

  3. Posted June 10, 2009 at 10:36 am | Permalink

    Thanks for the comprehensive list, I’ve been wondering are there any keyboard shortcuts for javascript. I’m not a very experienced user, but maybe they’ll appear to be useful

  4. Posted June 10, 2009 at 8:05 pm | Permalink

    wow ! this is really going to help to hide my code with ctrl+u … thnx for sharing … do u have something like to disable the viewsource file from Menu through script ?
    If so pls send me on my Email id.

    Thnks

  5. Posted June 10, 2009 at 8:23 pm | Permalink

    @saurabh

    hide your code? that’s gonna be a tricky one. and why would you wan’t to do that, anyway?

  6. Posted June 10, 2009 at 11:01 pm | Permalink

    If you’re using Prototype.js, you might want to check out LivePipe UI’s HotKey class. It makes this stuff easy. http://livepipe.net/extra/hotkey

  7. Posted June 10, 2009 at 11:38 pm | Permalink

    @Nathan L Smith: Didn’t knew that class. Thanks for sharing!

  8. Posted June 11, 2009 at 5:03 pm | Permalink

    Thanks for providing the list and for the article. This will go to my favorite bookmarks :)

  9. Posted June 12, 2009 at 8:15 pm | Permalink

    Hello there. Thank you for such an informative article. I am a Computer Engineering student. Now I have an edge to my classmates with this short cut keys. This will really help my in my course.

  10. Posted June 13, 2009 at 12:27 am | Permalink

    Wha web apps would you consider when making a shortcut using javascript?

  11. Posted June 13, 2009 at 3:19 pm | Permalink

    Thanks JBJ for sharing this. This is great contribution to the accessibility and simplicity of websites. I like it.

  12. Posted June 13, 2009 at 6:17 pm | Permalink

    thanks for this useful information but despite the many JavaScript libraries that are available today, I cannot find one that makes it easy to add keyboard shortcuts to your javascript app.

  13. Posted June 14, 2009 at 8:34 am | Permalink

    Superb tips all the shortcuts working fine, and to hide the view source which open by pressing ctrl+u
    is the classic one. These short cut keys definitely gonna cut short the task time. I’m gonna this blog bookmark..

  14. Posted June 14, 2009 at 2:28 pm | Permalink

    Thanks a lot for all the short cuts. They will definitely cut short my developing task and i will
    complete all tasks on time without delay..

  15. Posted June 15, 2009 at 2:55 pm | Permalink

    Thanks for this!

  16. Posted June 16, 2009 at 4:30 pm | Permalink

    This is a very useful and interesting you posted here. Thank you so much. I’ve learned a lot from it. Excellent posting. Good luck:-)

  17. Posted June 16, 2009 at 6:32 pm | Permalink

    Nice code sample and information. It is difficult to do it ourselves because there are thousand codes aailable but I always want to do it with my own hands(No malicious code threats).

  18. Posted June 17, 2009 at 7:59 am | Permalink

    Great help of code, always seeking for simple and soft code. it’s easy to implement for us again thx.

  19. Posted June 17, 2009 at 3:08 pm | Permalink

    Fantastic tips, I didnt even think about even trying this type of thing before I saw this

  20. Posted June 19, 2009 at 6:19 pm | Permalink

    Interesting. Of course I always add keyboard shortcuts to my actual programs, but hadn’t really thought about doing it in a web app.

    Do you know how it affects performance?

  21. Posted June 24, 2009 at 7:14 pm | Permalink

    Really the tips that mentioned above are incredible. They will definitely decrease my task
    of code implementation..

  22. Posted June 25, 2009 at 10:52 am | Permalink

    very informative sharing.. thanks for the lovely post.

  23. Posted September 17, 2009 at 4:43 pm | Permalink

    The which property is not supported by Internet Explorer.

  24. Posted October 21, 2009 at 4:50 pm | Permalink

    “The which property is not supported by Internet Explorer”

    Make sure u use “e.keyCode” insted of “wich” works for IE en FF

  25. Posted January 24, 2010 at 4:10 pm | Permalink

    I suggest a feature you implement onto your post… Alternating styled rows! it would help a lot!!!

  26. Posted January 24, 2010 at 4:44 pm | Permalink

    Why not use plain HTML and the accesskey attribute?

10 Trackbacks

  1. [...] Via CatsWhoCode [...]

  2. [...] Using keyboard shortcuts in Javascript [...]

  3. [...] Using keyboard shortcuts in Javascriptcatswhocode.com [...]

  4. [...] JavaScript – Using keyboard shortcuts in Javascript (Suggested by Smashing Magazine) [...]

  5. [...] For more and keyboard shorcuts codes click here> [...]

  6. [...] articulo original fue publicado en CatsWhoCode, traducido por [...]

  7. [...] Using keyboard shortcuts in Javascript If you want to enhance your web app, Javascript keyboards shortcuts is definitely something to consider. In this article, you’ll learn to use JS keyboard shortcuts, with and without the JQuery framework. [...]

  8. [...] Using keyboard shortcuts in Javascript If you want to enhance your web app, Javascript keyboards shortcuts is definitely something to consider. In this article, you’ll learn to use JS keyboard shortcuts, with and without the JQuery framework. [...]

  9. [...] Using keyboard shortcuts in Javascript If you want to enhance your web app, Javascript keyboards shortcuts is definitely something to consider. In this article, you’ll learn to use JS keyboard shortcuts, with and without the JQuery framework. [...]

  10. [...] Using keyboard shortcuts in Javascript If you want to enhance your web app, Javascript keyboards shortcuts is definitely something to consider. In this article, you’ll learn to use JS keyboard shortcuts, with and without the JQuery framework. [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe without commenting

  • Smashing Network
WordPress Appliance - Powered by TurnKey Linux