i've been trying to capture document level key press events in a webpage but
$(document).bind('keydown', 'a', keyevent_cb);
fails to respond consistently in firefox. works great in IE (which is kind of a trip). any remendations? i've attempted other solutions without jquery and they also fail for firefox.
so i'm open to any result that works consistently (jquery or not). thanks in advance.
i've been trying to capture document level key press events in a webpage but
$(document).bind('keydown', 'a', keyevent_cb);
fails to respond consistently in firefox. works great in IE (which is kind of a trip). any remendations? i've attempted other solutions without jquery and they also fail for firefox.
so i'm open to any result that works consistently (jquery or not). thanks in advance.
Share Improve this question edited Dec 14, 2011 at 22:19 Tim Down 325k76 gold badges460 silver badges542 bronze badges asked Dec 14, 2011 at 19:27 ct_ct_ 1,2274 gold badges21 silver badges35 bronze badges 4-
What does
keyevent_cb
do? Is there the suggestion of any errors therein? – David Thomas Commented Dec 14, 2011 at 19:29 - it's just a call back function. prints an alert box. – ct_ Commented Dec 14, 2011 at 19:32
- Do you want to fire the callback on all keypress events or only under certain conditions? – Phil Klein Commented Dec 14, 2011 at 19:39
- all key presses and subsequent. – ct_ Commented Dec 14, 2011 at 20:06
2 Answers
Reset to default 3The following attaches a keypress event listener to the body element:
$("body").on("keypress", function (e) {
// logic for key event here
});
With your keyevent_cb
callback, you could simply do:
$("body").on("keypress", keyevent_cb);
$(document).keypress(function(e)
{
switch(e.which)
{
// user presses the "a"
case 97: doSomething(); break;
}
});