Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping Mouse to Touch Events for testing Mobile Websites in Desktop browsers

I'm currently developing a website that should also work on mobile devices. But since I'm (of course) heavily using Javascript, I would prefer to have Desktop-based testing environment (FireFox, FireBug, etc.). Is there some way to map Mouse Events to Touch Events to be able to test the Website in a Desktop Browser, but "simulating" all the touch stuff as if it were a Mobile Device?

I've seen many libraries/functions to do it the other way around, but that's not what I want.

like image 515
Laph Avatar asked Feb 15 '11 08:02

Laph


People also ask

Does Touchstart work on desktop?

The touchstart event occurs when the user touches an element. Note: The touchstart event will only work on devices with a touch screen.

Can I use touch event?

Touch events are supported by Chrome and Firefox on desktop, and by Safari on iOS and Chrome and the Android browser on Android, as well as other mobile browsers like the Blackberry browser.

What are touch events react?

Touch events consist of three interfaces ( Touch , TouchEvent and TouchList ) and the following event types: touchstart - fired when a touch point is placed on the touch surface. touchmove - fired when a touch point is moved along the touch surface. touchend - fired when a touch point is removed from the touch surface.


1 Answers

I use a method where I map the keyboard to screen gestures. For example, on a site I'm working on, I want the page to go back if I swipe left and go next if I swipe right. I'm using the jQuery Mobile API.

First my debugger tooler:

$(document).keypress(function(event) {
     // Simulate Left Flick (A)
     if (event.which == '97') {
          alert('LEFT FLICK');
          SomeFunction1();
     }
     // Simulate Right Flick (D)
     if (event.which == '100') {
          alert('RIGHT FLICK');
          SomeFunction2();
     }
     });

My pages have the following template

$( "#Page" )
     .live('swipeleft',function() {
          SomeFunction1();
     })
     .live('swiperight',function() {
          SomeFunction2()
     });

If you want each page to do something different, you should tie the keypress object to your page. You're code would look something like this.

$( "#Page" )
     .live('swipeleft',function() {
          SomeFunction1();
     })
     .live('swiperight',function() {
          SomeFunction2()
     })
     .keypress(function(event) {
          // Simulate Left Flick (A)
          if (event.which == '97') {
               alert('LEFT FLICK');
               SomeFunction1();
          }
          // Simulate Right Flick (D)
          if (event.which == '100') {
               alert('RIGHT FLICK');
               SomeFunction2();
          }
     });

You can map other keys to other gestures by simply changing the event.which == "#" in the debugger code.

Hope that helps!

like image 178
Jesse E. Stark Avatar answered Nov 15 '22 11:11

Jesse E. Stark