简体   繁体   中英

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. For example, if someone presses the A key, a menu opens.

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? 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. 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事件处理程序附加到文档对象,该文档对象在用户点击任何键后懒洋洋地附加事件处理程序。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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