开发者

Is there a way to catch all keyboard input in a browser?

开发者 https://www.devze.com 2022-12-21 18:33 出处:网络
Is there a way to catch all keyboard input in a browser? Im guessing it would have to be JavaScript. My problem is that I have a USB Bar Code Scanner which the computer treats as a second keyboard. I

Is there a way to catch all keyboard input in a browser? Im guessing it would have to be JavaScript.

My problem is that I have a USB Bar Code Scanner which the computer treats as a second keyboard. I dont want the user to have to click on the input box for bar codes to be entered in it. But I also want them to开发者_JAVA百科 be able to hit key short cuts that perform an action that does not display that input in the text box. Also can the f1 - f12 keys be used in this manner or are they reserved for the browser itself?


keydown will fire for most keys (certainly more than keypress) in most browsers (some, such as Safari 3.0, won't fire any events for modifier keys such as Shift) and all keys in recent browsers. The behaviour of function keys is not a good thing to rely on though.

The following page is an excellent reference for key events in JavaScript: http://unixpapa.com/js/key.html


You can handle the keypress event for the document object, which will receive almost all keyboard input.

The exact behavior of function keys will depend on the browser.


I did this before. I used a timer. I give the input box focus and start the timer after the first keypress is detected, then if no other keys are pressed within 100 milliseconds I submit the form. The barcode scanner "types" pretty quickly.

You can also intercept the character codes to detect the F-keys.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号