admin管理员组文章数量:1400123
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;
}
});
本文标签: jquerycapturing document level keypress events with javascriptStack Overflow
版权声明:本文标题:jquery - capturing document level keypress events with javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744197165a2594794.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论