Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do desktop browsers support touch events?

Tags:

html

touch

I'm trying to get up to speed on HTML5 by prototyping an extremely simple drawing app using <canvas> and touch events like ontouchstart and ontouchmove.

This is displaying and working fine in an Android emulator where I use mouse clicks to simulate touch events. However, it doesn't work at all when running in my desktop browser (Safari 5.1.1, Firefox 7 on Windows).

I thought the mouse click events would be interpreted as touch events like they are when running within the emulator.

I wonder now if perhaps desktop browsers simply don't support touch events.

like image 742
Justin Avatar asked Nov 22 '11 14:11

Justin


2 Answers

It's actually the other way round. Mobile browsers translate touch to mouse events for legacy support. If it needs to work on both mobile and desktop, and you don't really need touch events, you could just use mouse events instead. Failing that, bind both touch and mouse events and have the touch event cancel the mouse event. This way on mobile devices the touch fires and mouse doesn't. On desktop the touch doesn't fire and the mouse will.

For pilau, here's a simple example of cancelling the click event on mobile devices. Please note the question is more about drawing which would involve click dragging, which would require a bit more code for detection, but the basic idea is the same, bind the events you need to handle. Then e.preventdefault() will stop the mobile browsers from emulating click type events.

$('#element').on('touchend click', function(e){

   // Do your onclick action now

   // cancel the onclick firing
   e.preventDefault();
});
like image 150
Lee Avatar answered Sep 21 '22 21:09

Lee


Firefox 6 and above supports the touch events. See the compatibility table here.

MDN article on Touch Events

EDIT : Are you testing with a touchscreen or the mouse? The mouse events do not automatically translate to touch events. See this post for how to simulate touch events with a mouse.

like image 37
pradeek Avatar answered Sep 23 '22 21:09

pradeek