Archive

JavaScript keystroke listener

Here is nice piece of JavaScript that allows you to add keystroke listener to your JavaScript application

//keystroke listener
var keyStroker = function(f, k, e){

     document[e] = function(event){
        
        var keycode;
        
        //IE hack
        if ( typeof event == "undefined" ) {
            event = window.event;
        }
        
        //IE different event object
        if ( typeof event.which == "undefined" ) {
            keycode = event.keyCode;
        }
        else{
            keycode = event.which;
        }
        
        //run callback if particular key was clicked
        if(keycode==k){
            f();
        }
    };
};

keyStroker(function(){
    //write code here
}, 192, "onkeyup");

Comments:

Wordpress Themes
8/11/2010 3:32 PM
Good fill someone in on and this enter helped me alot in my college assignement. Gratefulness you for your information.

gourmet coffee gift
8/24/2010 1:49 PM
I can see that you are an expert at your field! I am launching a website soon, and your information will be very useful for me.. Thanks for all your help and wishing you all the success.

coffee
8/25/2010 11:29 PM
i have been following this blog for some time now, good job by the way

blue dolphin pools
8/28/2010 1:23 AM
I can' t but agree.I always wanted to write in my site something like that but I guess you' r faster.

business lending
8/29/2010 9:03 AM
Aw. this was a really quality post.

Owen J
12/16/2010 7:41 PM
Thanks! I was looking for a quick reference to add key listeners, and found just that.