处理基于触摸的事件 [英] Handling touch-based events

查看:29
本文介绍了处理基于触摸的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前为我的触摸事件准备了这个:

I currently have this for my touch events:

if( 'ontouchstart' in document.body) {
    usevkeys = true;
    canvas.addEventListener("touchstart",function(e) {evt.call(this,e);},false);
    canvas.addEventListener("touchend",function(e) {evt.call(this,e);},false);
    canvas.addEventListener("touchmove",function(e) {evt.call(this,e);},false);
}
else {
    canvas.addEventListener("mousemove",function(e) {evt.call(this,e);},false);
    canvas.addEventListener("click",function(e) {evt.call(this,e);},false);
}

这在我的笔记本电脑和手机上都可以正常工作.但是,我想知道,这在同时具有触摸屏和普通鼠标的环境中会如何反应?鼠标是否触发触摸事件,就像手机触发 mousemove 事件一样?

This works fine on my laptop, and on my phone. However, I have to wonder, how would this react in an environment that has both a touchscreen and a normal mouse? Does the mouse trigger touch events, like the phone triggers mousemove events?

我该怎么做才能确保它有效?

What can I do to make sure it works?

推荐答案

touchstart 会在 touchend 触发后立即触发 click 事件(-> 如果它没有被取消).所以你应该删除 else 子句,应该没问题!

a touchstart triggers the click event as soon as touchend has fired (-> if it hasn't been canceled). So you should just remove the else clause and should be fine!

微软在 IE10 中采用不同的方式处理他们的触摸事件,将所有指针设备统一到 指针和手势事件,您可以在其中检查它是否被鼠标、笔或手指 - 或者将来可能是 kinect 风格的设备触发.

Microsoft went a different way with their touch events in IE10 to harmonize all pointer-devices into Pointer and gesture events where you can check if it has been fired by a mouse, pen or a finger - or perhaps a kinect-style device in the future.

这篇关于处理基于触摸的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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