检测浏览器网页中是否有键盘/方向键

发布于 2024-10-12 09:47:57 字数 307 浏览 2 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

眼藏柔 2024-10-19 09:47:57

不需要任何用户代理嗅探、配置选项或任何类型的猜测。只需这样做:

  1. 有一个标题屏幕,上面写着“按继续”。
  2. 单击或按键时,隐藏触摸控件并开始游戏。
  3. 触摸时,显示触摸控件并开始游戏。

您甚至不需要向用户提及该选项,并且您可以完美地自动检测他们的首选控件。

No need for any user-agent sniffing, config options or any kind of guessing. Just do this:

  1. Have a title screen which says "press to continue".
  2. On click or key press, hide touch controls and start game.
  3. On touch, show touch controls and start game.

You never even needed to mention the option to the user and you auto-detected their preferred control perfectly.

醉南桥 2024-10-19 09:47:57

将特征检测与 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.

追风人 2024-10-19 09:47:57

不要尝试猜测,而是将其作为配置选项供用户选择。

Instead of trying to guess, make it a config option for the user to choose.

心作怪 2024-10-19 09:47:57

如果您只有箭头(左/右/上/下),您可能会考虑在游戏字段中添加触摸事件?这显然不会占用空间,因为它位于游戏之上,因此它可以“永远在线”。

  • 计算机用户甚至不知道它在那里,尽管我猜他/她可以用它们来用鼠标玩你的游戏。

  • 另一方面,触摸设备用户可以更轻松地使用“区域”(例如中上、中左、中下和中右),因为……好吧……触摸而不是使用鼠标。

这可能需要一些解释,因为您可能不希望用户看到这些点,但这感觉像是一个有效的选项。

即使您有 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.

清晰传感 2024-10-19 09:47:57

查找特定于触摸的事件,例如 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/

合约呢 2024-10-19 09:47:57

添加一个按钮来切换屏幕键盘的显示,而不是默认显示屏幕键盘。

为屏幕键盘提供调整大小的功能也可能是谨慎的做法。

编辑回答问题:

如果您的大多数用户将使用计算机,则默认情况下键盘应隐藏;

如果您的大多数用户将使用移动设备,则默认情况下键盘应可见。

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.

稚气少女 2024-10-19 09:47:57

您可以考虑检查显示尺寸。如果显示尺寸小于特定尺寸,则可以假设它是移动设备并且可以显示箭头按钮。否则使用键盘按钮。

您还可以保留一个选项,以便用户可以根据需要手动设置。

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.

拍不死你 2024-10-19 09:47:57

您可以使用 javascript 找出 windows 视口的高度,然后使用 if 语句表示:

if ($(window).height() <= "960")) {
    //Your code to display keyboard controls
    //P.S. 960 is height of iPhone 4+ screen
}

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:

if ($(window).height() <= "960")) {
    //Your code to display keyboard controls
    //P.S. 960 is height of iPhone 4+ screen
}

Edit: Left out ) at end of $(window).height() <= "960"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文