David
David

Reputation: 14029

How to perform action on keypress?

My code works perfect if I want to perform an action when pressing the ctrl key...

$(document).keydown(function(e){
    if(e.ctrlKey){
        if($('.selected').parent().next().length == 0){
            switchTabs($('.selected').parent().parent().find('a:first'));
            return false;
         }else{
            switchTabs($('.selected').parent().next().children('a'));
            return false;
        }
    }
});

However I only want this code to activate when I press only the right control button...

if(e.rightctrlkey) 

doesn't work... if there is a correct method, what is it?

Upvotes: 0

Views: 659

Answers (2)

Tim Down
Tim Down

Reputation: 324707

You won't be able to do this in general. You can do it in IE only:

// Assuming you have a key event stored in variable 'e':
if (e.ctrlLeft) {
    alert("Left!");
} else if (e.ctrlRight) {
    alert("Right!");
}

Upvotes: 1

vikmalhotra
vikmalhotra

Reputation: 10071

Make use of event.keyLocation attribute, Left=0, Right=1. Look over here for details.

Also look over in this stackoverflow question.

Upvotes: 0

Related Questions