With jQuery, how do I find out which key was pressed when I bind to the keypress event?
$(\'#searchbox input\').bind(\'keypress\', function(e) {});
>
edit: This only works for IE...
I realize this is an old posting, but someone might find this useful.
The key events are mapped, so instead of using the keycode value you can also use the key value to make it a little more readable.
$(document).ready( function() {
$('#searchbox input').keydown(function(e)
{
setTimeout(function ()
{
//rather than using keyup, you can use keydown to capture
//the input as it's being typed.
//You may need to use a timeout in order to allow the input to be updated
}, 5);
});
if(e.key == "Enter")
{
//Enter key was pressed, do stuff
}else if(e.key == "Spacebar")
{
//Spacebar was pressed, do stuff
}
});
Here is a cheat sheet with the mapped keys which I got from this blog