处理移动浏览器上的 onTouchDown 和台式机/笔记本电脑上的 onMouseDown?
我正在开发一个带有 HTML5 画布的网页游戏应用程序,它必须对“点击”事件(一般意义上的)做出反应。
在移动平台(或具有触摸功能)上,这意味着对 onTouchDown 做出反应,在台式机/笔记本电脑平台(使用鼠标或垫)上,这意味着对 onMouseDown 作出反应。
问题是,如果我处理这两个事件,那么有时相同的“点击”会导致两个事件被触发,所以我得到了双重信号。
处理这个问题的最佳方法是什么?
目前,在我收到第一个触摸事件时,我会关闭鼠标事件,但如果平台同时支持触摸和鼠标事件,这可能有点繁琐。鼠标点击(如果是带有键盘/键盘/鼠标的 Android 平板电脑)
测量触摸和点击之间的延迟单击以忽略触摸后的点击效果不太好,因为在某些情况下用户可能会高频触摸/点击,因此过长的延迟会导致双击/点击丢失,而过短的延迟会导致通过偶尔会漏掉双重信号。
我研究过用户代理检测,但这似乎非常脆弱(有许多用户代理),并且不能解决平台同时具有触摸和触摸功能的情况。鼠标垫。
I'm developing a web game application with a, HTML5 canvas that has to react to "click" events (in the general meaning).
On a mobile platform (or touch-capable), that means reacting to onTouchDown, on a desktop/laptop platform (with a mouse or pad), that means reacting to onMouseDown.
Problem is, if I handle both events, then sometimes the same "click" will result in both events getting fired, so I get a double signaling.
What would be the best way to handle that?
Currently, upon the first touch event I receive, I turn off mouse events, but that may be a bit heavy handed if the platform supports both touch & mouse clicks (f.i. an Android tablet with attached keyboard/pad/mouse)
Measuring the delay between touch & click to ignore a click after a touch doesn't work too well, as there are circumstances where user may touch/click at a high frequency, so a too long delay leads to dropped double taps/clicks, and a too short delay lets through the occasional double signaling slipping through.
I've looked at user-agent detection, but that seems quite fragile (many user agents out there), and doesn't solve the cases where the platform has both touch & mouse/pad.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许您的建议的组合:
,检测点击的 x,y 坐标会减少误报。因此,如果依次发生的两个事件(点击和单击)获得相同的坐标,则它们的处理方式相同。
或者,如果用户想在设备上使用鼠标或触摸(如果两者都受支持),则可以让用户进行切换(通过某种选项屏幕)。显示一条警告消息,例如说
您使用的是触摸设备,因此我们启用了触摸事件,如果您使用的是鼠标,请查看选项
或类似内容。大多数用户会对自动选择感到满意,每个不满意的人都可以更改它。
Maybe a combination of your suggestion:
And detecting the x,y coordinates of the tap would decrease the false positives. So if both events (tap & click) happening in sequence got the same coordinates they are handled as the same.
Or maybe let the user switch (through some sort of options screen) if he wants to use mouse or touch for his device if both is supported. Display a warning message for example saying
you're on a touch device so we enabled touch events, if you're using a mouse please see options
or something like that.Most users would be happy with the auto-choice and everybody unhappy can change it.