在处理之前用jQuery拦截所有按键 [英] Intercepting all key presses with jQuery before they are processed
问题描述
我有一个网页,我需要拦截所有按键,然后才能调度到dom中有焦点的任何元素。当用户键入类似 ~goto:/index.html:
(实际上它将来自条形码扫描仪)时,我需要在<$ c后捕获按键$ c>〜在我到达第二个:
时解析它们。我设法用 $(文件).keypress()
获得了如下内容:
I have a web page and I need to intercept all keypresses before they get dispatched to whatever element in the dom has the focus. When the user types something like ~goto:/index.html:
(which in fact will come from a barcode scanner) I need to capture the key presses after the ~
to parse them once I get to the second :
. I've managed to get something with the $(document).keypress()
as follows:
$(document).ready(function() {
$(document).keypress(function(e) {
// do the processing here and
// return false when ignoring keystrokes
});
});
问题是我在进入焦点元素后得到了按键。无论如何得到它以便我实际上可以拒绝它?
The problem is that I get the keypress after it has gone to the focussed element. Anyway to get it before so that I can in fact reject it?
推荐答案
你可能会有更好的运气 keydown 它应该在事件发生前触发
You will probably have better luck with keydown it should fire before the event
也这样做
$(document).keydown(function(e) {
e.preventDefault();
});
调用preventDefault将停止按键的正常进程。实际上,您可以使用按键,只需使用preventDefault来完成拦截需求。
calling preventDefault will stop normal process of the key press. Actually, you may be able to stay with keypress and just use the preventDefault to finish your interception needs.
这篇关于在处理之前用jQuery拦截所有按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!