Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to differentiate between key "5" and "%" on keydown in jquery? [duplicate]

I want to be able to have a trigger on keypress for %. So far, I have this:

$(iframeDocument).find('body').on('keydown', function(event) {
    if (event.which == 53) {
        alert("% pressed");
    }
});

But, it will also trigger when I press "5". How can I differentiate between the two?

like image 897
coffeeak Avatar asked Feb 07 '23 12:02

coffeeak


1 Answers

You can use shiftKey property in the event object to check if the SHIFT is pressed when the event occurred.

var keyCode = event.which || event.keyCode;

if (event.shiftKey && keyCode === 53) {
    // Shift key is pressed
    console.log('% pressed');
}
like image 136
Tushar Avatar answered Feb 11 '23 01:02

Tushar