简体   繁体   English

HTML5; 在Javascript中检测物理键盘的存在

[英]HTML5; detect presence of physical keyboard in Javascript

What is the most reliable way to detect if a user has a physical keyboard?检测用户是否拥有物理键盘的最可靠方法是什么?

An idea is that since there is no physical keyboard window.onkeydown could be undefined (instead of null ).一个想法是,由于没有物理键盘window.onkeydown可能是undefined (而不是null )。 But because of the virtual keyboard I'd suppose it to not be the case.但由于虚拟键盘,我认为情况并非如此。 I didn't test though.虽然我没有测试。

My goal is to have input[type='number'] of an Online Timer to be replaced with wheel pickers if the user doesn't have a keyboard.如果用户没有键盘,我的目标是将在线计时器的input[type='number']替换为轮选择器。

I see two approaches.我看到两种方法。

The first approach would be to listen to every mouse and keyboard events.第一种方法是监听每个鼠标和键盘事件。 A user having a mouse is likely to also have a keyboard.拥有鼠标的用户很可能也拥有键盘。 A user going to a website is likely to move the mouse.访问网站的用户很可能会移动鼠标。

The second approach is to check the User Agent to read what Operating System is running and assume that the Android and iOS devices don't posses any keyboard.第二种方法是检查用户代理以读取正在运行的操作系统,并假设 Android 和 iOS 设备没有任何键盘。 For Windows 8 I don't see how the User Agent could help, since windows 8 runs on both tablets and desktops/notebooks.对于 Windows 8,我看不出用户代理如何提供帮助,因为 Windows 8 可在平板电脑和台式机/笔记本电脑上运行。

I'd rather have a more elegant solution though.不过,我宁愿有一个更优雅的解决方案。

This gets really tricky with devices like the Microsoft Surface, which come with a keyboard, mouse, and have a touch screen.对于 Microsoft Surface 等配备键盘、鼠标和触摸屏的设备,这会变得非常棘手。 And you may mix all 3 input modes at any given time, or add another input method like a stylus.您可以在任何给定时间混合所有 3 种输入模式,或添加另一种输入法,如手写笔。

Microsoft is taking the approach of abstracting input events as "pointer events". Microsoft 正在采用将输入事件抽象为“指针事件”的方法。 This modelhas been submitted to the W3C .该模型已提交给 W3C This should give you an idea of the trend in managing input.这应该让您了解管理输入的趋势。

Still, I find it's handy to see if touch is available and operate under the assumption that—if it is—the user will use touch input at least some of the time.尽管如此,我发现查看触摸是否可用并在假设用户至少在某些时候使用触摸输入的情况下进行操作是很方便的。 This can lead to design decisions to eliminate things which are completely touch-unfriendly even though that may mean compromising on mouse/keyboard functionality.这可能导致设计决策消除完全不利于触摸的东西,即使这可能意味着损害鼠标/键盘的功能。

In your specific case, I would take a hard look at whether or not you even need to replace input[type=number] with a custom control.在您的特定情况下,我会仔细研究您是否甚至需要用自定义控件替换input[type=number] Most touch devices are reasonably modern, and many have custom, touch-friendly versions of the standard HTML inputs.大多数触摸设备都相当现代,并且许多具有标准 HTML 输入的自定义、触摸友好版本。

Also keep in mind accessibility scenarios which native controls probably support well "out of the box".还要记住本机控件可能很好地支持“开箱即用”的可访问性方案。

If you decide to implement a custom control, then I would advise detecting touch features and showing your custom control whether or not other input mechanisms are present.如果您决定实现自定义控件,那么我建议检测触摸功能并显示您的自定义控件是否存在其他输入机制。 This means ensuring that the custom control is (at minimum) touch/keyboard/mouse friendly.这意味着确保自定义控件(至少)对触摸/键盘/鼠标友好。

Here's my current touch test (with the disclaimer that it could break tomorrow and certainly hasn't been tested on every device):这是我当前的触摸测试(免责声明它明天可能会中断,并且肯定没有在每台设备上进行测试):

var supportsTouch = ("ontouchstart" in window) || window.navigator.msMaxTouchPoints > 0;

it's more reliable to detect a touchscreen and show the special widget then;检测触摸屏并显示特殊小部件更可靠; just make sure the keyboard is still usable with your fancy widget for accessibility's sake.为了可访问性,只需确保键盘仍然可以与您喜欢的小部件一起使用。

You might be able determine the keyboard type by checking the operating system or even the screen dimensions.您可以通过检查操作系统甚至屏幕尺寸来确定键盘类型。 Android, iOS, and all devices with small screens tend not to have physical keyboards. Android、iOS 和所有小屏幕设备往往没有物理键盘。

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

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