问题描述
我想问一个人如何在不关注特定领域的情况下阅读html页面中的按键.是否有一些事件可以挂接读取输入的方法?
I want to ask how can someone read keystrokes while in a html page without having focus in a particular field. Is there some event that i can hook a method that reads input?
谢谢.
PS.如果有帮助,我们可以使用JQuery.
PS. We use JQuery if that helps.
推荐答案
使用document.onkeypress
(或兄弟姐妹onkeydown
和onkeyup
):
Use document.onkeypress
(or the siblings onkeydown
and onkeyup
):
document.onkeypress = function (event) { ... };
jQuery 为这些事件提供的服务,例如
jQuery offers for these events, e.g.,
$(document).keypress (function (event) { ... });
这将捕获您网站上的所有个按键事件.在函数内部,您可以使用event.keyCode
获取所按键的数字代码.请注意,存在一些浏览器不兼容性,尤其是与keypress
事件有关.有关FF如何处理的信息,请参见 developer.mozilla.com .
This will catch all key press events on your site. Inside the function you can get the numeric code of the pressed key with event.keyCode
. Be aware, that there are some browser incompatibilities, especially with the keypress
event. See developer.mozilla.com for how FF handles it.
干杯
这篇关于如何在没有焦点的情况下使用javascript读取html页面中的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!