HTML5;在Javascript中检测物理键盘的存在 [英] HTML5; detect presence of physical keyboard in Javascript

查看:147
本文介绍了HTML5;在Javascript中检测物理键盘的存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

检测用户是否有物理键盘的最可靠方法是什么?

What is the most reliable way to detect if a user has a physical keyboard?

一个想法是,因为没有物理键盘 window.onkeydown 可以是 undefined (而不是 null )。但是由于虚拟键盘,我认为情况并非如此。我没有测试过。

An idea is that since there is no physical keyboard window.onkeydown could be undefined (instead of null). But because of the virtual keyboard I'd suppose it to not be the case. I didn't test though.

我的目标是输入输入[type ='number'] 如果用户没有键盘,则用轮式选择器替换在线计时器

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.

推荐答案

使用键盘,鼠标和触摸屏等微软Surface等设备变得非常棘手。并且您可以在任何给定时间混合所有3种输入模式,或者添加另一种输入方法,如手写笔。

This gets really tricky with devices like the Microsoft Surface, which come with a keyboard, mouse, and have a touch screen. And you may mix all 3 input modes at any given time, or add another input method like a stylus.

Microsoft正在采用抽象输入事件作为指针事件的方法。此模型已提交给W3C 。这应该可以让你了解管理输入的趋势。

Microsoft is taking the approach of abstracting input events as "pointer events". This model has been submitted to the W3C. This should give you an idea of the trend in managing input.

但是,我觉得看看触摸是否可用并且在假设—如果它是&mdash的情况下运行是很方便的;用户至少在某些时候会使用触摸输入。这可能导致设计决定消除完全触摸不友好的东西,即使这可能意味着鼠标/键盘功能受损。

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.

在您的具体情况下,我会采取难以确定您是否需要使用自定义控件替换输入[type = number] 。大多数触摸设备都相当现代,许多触摸设备都有标准HTML输入的自定义,触摸友好版本。

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. Most touch devices are reasonably modern, and many have custom, touch-friendly versions of the standard HTML inputs.

还要记住本机控件可能支持的可访问性场景开箱即用。

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;

这篇关于HTML5;在Javascript中检测物理键盘的存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆