检测浏览器网页中是否有键盘/方向键
我有一个 HTML+JavaScript 的全屏游戏,它使用箭头键作为主要控件。这不能在无键盘的 Android 设备上使用(我没有在 iOS 上测试过),而且即使软键盘有方向键也会占用不必要的空间。因此,我添加了屏幕控制按钮。然而,这些按钮在桌面浏览器上是不必要的(而且大得离谱),所以我希望它们不会弹出,除非需要它们。
我可以使用哪些启发式方法来决定是否需要它们(即,用户是否不可能或难以输入箭头键事件)除了识别特定的用户代理(这很简单,但不是面向未来的)?
我当然会允许用户隐藏/显示按钮;我正在寻找有用的启发法来选择默认设置。
I have a full-screen game in HTML+JavaScript, which uses the arrow keys as primary controls. This cannot be used on keyboardless Android devices (I haven't tested on iOS), and even if the soft keyboard had arrow keys it would take up unnecessary space. Therefore, I have added onscreen control buttons. However, the buttons are unnecessary (and absurdly large) on desktop browsers, so I would like them to not pop up unless they are needed.
What heuristics can I use to decide whether they are needed — that is, whether it is impossible or awkward for the user to input arrow-key events — other than recognizing specific User-Agents (which is straightforward, but not future-proof)?
I will of course allow the user to hide/show the buttons; I am looking for useful heuristics for choosing the default setting.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
不需要任何用户代理嗅探、配置选项或任何类型的猜测。只需这样做:
您甚至不需要向用户提及该选项,并且您可以完美地自动检测他们的首选控件。
No need for any user-agent sniffing, config options or any kind of guessing. Just do this:
You never even needed to mention the option to the user and you auto-detected their preferred control perfectly.
将特征检测与 Modernizr 结合使用:http://www.modernizr.com/docs/#touch
虽然这不是检查用户是否有键盘的可靠方法,但查看浏览器是否支持触摸绝对是可靠的方法。
Use feature detection with Modernizr: http://www.modernizr.com/docs/#touch
While this is not a reliable way to check if the user has a keyboard it is definitely reliable to see if the browser is capable of touch.
不要尝试猜测,而是将其作为配置选项供用户选择。
Instead of trying to guess, make it a config option for the user to choose.
如果您只有箭头(左/右/上/下),您可能会考虑在游戏字段中添加触摸事件?这显然不会占用空间,因为它位于游戏之上,因此它可以“永远在线”。
计算机用户甚至不知道它在那里,尽管我猜他/她可以用它们来用鼠标玩你的游戏。
另一方面,触摸设备用户可以更轻松地使用“区域”(例如中上、中左、中下和中右),因为……好吧……触摸而不是使用鼠标。
这可能需要一些解释,因为您可能不希望用户看到这些点,但这感觉像是一个有效的选项。
即使您有 4 个按钮和一个“火”,您也可以这样做,例如添加一个“中间”部分。
If you have only arrows (left/right/up/down) you might consider adding touch-events inside the game field? This would not take up space obviously as it is layered on top of the game, so it could be 'always on'.
A computer user would not even know it is there, though he/she could use them to play your game with a mouse I guess.
The touch-device user on the other hand can much more easily use the "areas" (mid top, mid left, mid bottom and mid right for instance) because of .. well.. touching instead of using a mouse.
This might need some explaining, as you probably would not want the points to be visible to the user, but it feels like a valid option.
Even if you have 4 buttons and a 'fire', you could do this, for instance by adding a 'middle' section.
查找特定于触摸的事件,例如 touchstart 或gesturestart,并在检测到时显示屏幕控件。
http://developer.apple.com/ Library/safari/#documentation/AppleApplications/Reference/SafariWebContent/HandlingEvents/HandlingEvents.html
我不确定 system-info api 是否已由任何浏览器实现:
http://www.w3.org/TR/system-info-api/
look for touch specific events such as touchstart or gesturestart and show the onscreen controls if detected.
http://developer.apple.com/library/safari/#documentation/AppleApplications/Reference/SafariWebContent/HandlingEvents/HandlingEvents.html
I am not sure if the system-info api has been implemented by any browsers:
http://www.w3.org/TR/system-info-api/
添加一个按钮来切换屏幕键盘的显示,而不是默认显示屏幕键盘。
为屏幕键盘提供调整大小的功能也可能是谨慎的做法。
编辑回答问题:
如果您的大多数用户将使用计算机,则默认情况下键盘应隐藏;
如果您的大多数用户将使用移动设备,则默认情况下键盘应可见。
rather than displaying the on-screen keyboard by default, add a button to toggle the display of the on-screen keyboard.
It might also be prudent to give the on-screen keyboard the ability to be resized.
Edit to answer question:
Keyboard should be hidden by default if most of your users are going to be on a computer,
Visible by default if most of your users are going to be on a mobile device.
您可以考虑检查显示尺寸。如果显示尺寸小于特定尺寸,则可以假设它是移动设备并且可以显示箭头按钮。否则使用键盘按钮。
您还可以保留一个选项,以便用户可以根据需要手动设置。
You can consider checking the display size. If the display size is smaller than a certain size, you can assume that it is a mobile device and can display the Arrow Buttons. Other wise use keyboard buttons.
You can also keep an option so that user can set this manually if needed.
您可以使用 javascript 找出 windows 视口的高度,然后使用 if 语句表示:
Edit: Left out ) at end of
$(window).height() <= "960"
You could use javascript to find out the height of the windows view port and then us an if statement saying:
Edit: Left out ) at end of
$(window).height() <= "960"