简体   繁体   English

如何通过Javascript检测用户是否有键盘?

[英]How do you detect if a user has a keyboard via Javascript?

I'm building a site where I'm globally binding certain keys to shortcuts using the keyup event. 我正在构建一个网站,我使用keyup事件将某些键全局绑定到快捷方式。 For example, if someone presses the A key, a menu opens. 例如,如果有人按下A键,则会打开一个菜单。

I don't want to bind these triggers on mobile devices (such as phones and tablets). 我不想在移动设备(如手机和平板电脑)上绑定这些触发器。 However, there's a number of people that use keyboards with their tablets. 但是,有很多人使用键盘和平板电脑。 How would I detect if a keyboard input exists via Javascript? 如何通过Javascript检测键盘输入是否存在? I know I can detect touch devices based on if touchstart exists, but that wouldn't work for people who are on tablets and have keyboards plugged in. I only want to bind the keyup events if a keyboard is present. 我知道我能够基于触摸的设备如果touchstart存在,但不会对人谁是在平板电脑和有插上键盘工作。我只是想绑定keyup事件如果键盘存在。 What is the best way to go about doing this? 这样做的最佳方法是什么?

Note: I know this is a nice-to-have, but I'd like to do it if possible. 注意:我知道这是一个很好的,但我想尽可能这样做。

Thanks in advance! 提前致谢!

您可以随时将一个catch-all事件处理程序附加到文档对象,该文档对象在用户点击任何键后懒洋洋地附加事件处理程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM