jQuery:我如何监听一般键盘输入?
问题描述:
我正在构建一个网站,在加载页面后,需要监听特定的键盘字符串。
I'm building a site that after the page is loaded, needs to listen for a particular keyboard string.
我感兴趣的事件实际上是一个扫描对象的扫描仪,但它作为键盘输入提供给网站,格式为 ~XXX 〜
。
The event I am interested in is actually a scanner scanning an object, but it presents to the site as keyboard input, of the form ~XXX~
.
我看到jQuery有一个你可以绑定的 keypress()
事件一个特定的对象。
I see jQuery has a keypress()
event that you can bind to a particular object.
但是如何在 $(文件).ready
之后听一般的键盘输入?
But how can I listen for general keyboard input, after $(document).ready
?
答
试试这个:
$(function() {
$(window).keypress(function(e) {
var key = e.which;
//do stuff with "key" here...
});
});
在 jsFiddle