views:

1053

answers:

2
$(document).ready(function() {
    $(document).keyup(function(e) {
        alert('This key: ' + e.keyCode);
    });
});

If an individual key is mapped to multiple characters, then the keyup event will execute multiple times for one key press.

Does anyone know how to identify just the physical act of pressing the key in this instance?

Cheers

+1  A: 

Using keyup, keys like ctrl will not trigger an event. keydown do though.

Using the codes below, I can capture almost all physical keys pressed down (I think).

Script:

$(document).ready(function(){
    $(document).keydown(function(e){
        $("#key").html($("#key").html() + " " + e.keyCode)
    });
});

Html:

<div id="key"></div>
o.k.w
I've tried keydown, keypress and keyup but none of them cature the actual single key press in this case.I am dealing with a situation where a string has been mapped to a key. So in this case a single key press generates a keydown/keypress/keyup event for each character in the string. I want to just capture the individual key press.
Che
@Che: if you are refering to bypassing keys mapped by hotkey utilities or the OS, I guess there's little or no chance of capturing the actual physical key press. Those utilities intercept the key actions at a lower level than what a typical browser does to the script at DOM level.
o.k.w
@o.k.w Yes that is exectly what I mean and I'm beginning to agree with you. I haven't found any information on how I might identify a single key press. Thanks mate.
Che
@Che: No prob, hope you can find a way out. Good luck!
o.k.w
A: 

Here is a nice link that discusses the different key events, as well as some nice charts on different browser implementation/event handling.

keydown, keypress, keyup

Hope that helps!

jaywon
Thanks for the link, I had read it earlier, unfortunately it doesn't help in my case but was interesting none the less.
Che