('ontouchstart' in window) returns true but no touch events

11,871

Can I tell from script if "Emulate touch events" is enabled or not ?

Try to check for more than one finger ;-)

Perhaps this helps?

window.addEventListener('touchstart', function(event) {
    var emulate = event.targetTouches.length == 2;
    alert(emulate ? true : false);
}, false);

BTW: 'ontouchstart' in window results in false (chrome v21.0.1180.60) unless you have closed the developer toolbar.

Fiddle

Share:
11,871
epeleg
Author by

epeleg

Trying to reach out to me? use "epeleg" on gmail.com.

Updated on June 04, 2022

Comments

  • epeleg
    epeleg over 1 year

    I am trying to write code that will run either in a browser or as a phonegap application. One of the things I need to do is to decide if I am in an environment that supports touch events or not.

    My current problem is that my Chrome (20.0.01132.47 m) returns true for ('ontouchstart' in window) but it does not fire the touch events.

    When I open the Developer tools settings dialog (the gear icon on the bottom right of the Developer Tools). I get an option to "Emulate touch events". When I check this option the browser fires the touch events. but when it is not checked it does not fire the events but the detection I use still says that the touch events are available.

    Can I tell from script if "Emulate touch events" is enabled or not?

    JQuery based answers are fine.

  • epeleg
    epeleg about 11 years
    I am not sure how this is supposed to help. If touchstart is not fired this code is never called anyway. maybe my Q wasn't clear enough - I am not trying to tell apart touch events that are emulated from "real" ones. I want to know if touchEvents are supported at all.
  • Vimil Saju
    Vimil Saju over 10 years
    why not just create a global flag which is initially set to false. in the ontouch event handler set the flag to true. So if the event fires, the flag gets set to true and you know the device has touch support
  • yckart
    yckart over 10 years
    @VimilSaju however you cannot detect if the event-handler-function was called during an emulated event, in this case chromes Emulate touch events-feature. So the global flag will changed on all devices, even on chrome for desktops.
  • Vimil Saju
    Vimil Saju over 10 years
    @yckart but if emulate touch events is turned on, it is deliberately turned on by the user for a specific reason. Isn't this same as a browser emulating a different user-agent (or an application trying to determine if its running on a VM or a real machine - very few applications would want to know that)? Why would any site want to know if the touch events are emulated or not? if the browser says touch events are supported then that is sufficient, How the support is provided (either through emulation or actual hardware support) should be irrelevant to the site.
  • yckart
    yckart over 10 years
    Yeah, you are completely right. However, this would not answer his question ;)
  • Ming-Tang
    Ming-Tang over 9 years
    I tried that a few times and Google Chrome, with all other tabs, crashed