How to find out the key pressed on keyup or keypress event when dollar key is pressed

Anoop picture Anoop · Mar 1, 2013 · Viewed 22.3k times · Source

The below method returns any key from A-Z and 0-9 and when I pressed shift key + 4 its returns the same code 52.

function keyUpEvent(e)//onkeyup event
{
    var chr = String.fromCharCode(e.keyCode);//converts the keycode into a character
    alert(chr);//alerts the character
}

But I need to show the $ or % when the respective key with combination of shift key is pressed.

Answer

Imperative picture Imperative · Mar 1, 2013

i'd go with something like this:

$(function () {
    $(document).on('keyup keydown keypress', function (event) {
        if (event.keyCode == 52 && event.shiftKey) {
            alert('foo');
        }
    });
});

jsfiddle