I want to get alerted whenever I press a key.
I\'ve tried:
$(\'body\').live(\'keyup\', function() {
alert(\'testing\');
});
Bu
Try using $("html")
or $("*")
instead of $("body")
. In order for the keyUp
event on body to fire, the body node or one of its children must be focused. You can accomplish this in your example by adding a text input and focusing the mouse to that input. What you really want is to capture any key press, so $("html")
should work.
Edit: I think your example might work, but in any case, to run the logic conditionally you might try this:
if ($(document.body).is(".focusOnKeypress")) {
$("html").live(...);
}
Or, I think this will also work:
$("body:not(.noFocusOnKeypress)").parent("html").live(...);