[RESOLVED]KeyDown and KeyUp

I would like a site listing all the keys on a standard keyboard All the KeyDown and KeyUp codes for each.

Also how to make an event out of these three key to input Capitals and Special Keys:

16 – Shift

17 – Cntl

18 – Alt

I posted the same answer to another related question yesterday, but if you are not looking for library that does the job for you then you may look up the source code of the below libraries since they do as you exactly want to

Mousetrap Libray

http://craig.is/killing/mice

Keypress Library

http://dmauro.github.io/Keypress/

If you are looking for a particular set of key codes, you could check out something like

this resource
(or just consider searching around for "key code list for javascript"). In general though, since you are already using jQuery (as per your questions location) capturing keyCodes should be fairly standardized and should generally
use the event parameter for your methods to access the key that was pressed :

$(selector).keyup(function(e) {
    // Get key pressed through the e.which function
    if(e.which == x){
        // Do something
    }
});

$(selector).keydown(function(e) {
    // Get key pressed through the e.which function
    if(e.which == x){
        // Do something
    }
});

$(selector).keypress(function(e) {
    // Get key pressed through the e.which function
    if(e.which == x){
        // Do something
    }
});

If you need to check if Shift, Ctrl or Alt is being held, you can do that as well :

$(selector).keyup(function(e) {
    // Get key pressed through the e.which function and if the shift key was held
    if(e.shiftKey && e.which == x){
        // Do something
    }

    // Get key pressed through the e.which function and if the alt key was held
    if(e.altKey && e.which == x){
        // Do something
    }

    // Get key pressed through the e.which function and if the alt key was held
    if(e.ctrlKey && e.which == x){
        // Do something
    }
});

As you can see above, jQuery allows you to easily capture both the key and if a particular function key is held through the e.shiftKey, e.ctrlKey and e.altKey properties.

Leave a Reply