Skip to content

Instantly share code, notes, and snippets.

@michael-nischt
Created February 20, 2016 10:37
Show Gist options
  • Save michael-nischt/f2c1fe1535f2b7f62323 to your computer and use it in GitHub Desktop.
Save michael-nischt/f2c1fe1535f2b7f62323 to your computer and use it in GitHub Desktop.
Simple touch to (left) mouse click shim
// This is free and unencumbered software released into the public domain.
//
// Anyone is free to copy, modify, publish, use, compile, sell, or
// distribute this software, either in source code form or as a compiled
// binary, for any purpose, commercial or non-commercial, and by any
// means.
//
// In jurisdictions that recognize copyright laws, the author or authors
// of this software dedicate any and all copyright interest in the
// software to the public domain. We make this dedication for the benefit
// of the public at large and to the detriment of our heirs and
// successors. We intend this dedication to be an overt act of
// relinquishment in perpetuity of all present and future rights to this
// software under copyright law.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// For more information, please refer to <http://unlicense.org/>
// Simple touch to (left) mouse click shim by Michael Nischt
/*jslint browser: true*/
(function (document) {
"use strict";
function touchHandler(event) {
var type = "";
switch (event.type) {
case "touchstart":
type = "mousedown";
break;
case "touchmove":
type = "mousemove";
break;
case "touchend":
case "touchcancel":
case "touchleave":
type = "mouseup";
break;
default:
return;
}
var first = event.changedTouches[0]; //NOTE: or use the average of all
var simulatedEvent = document.createEvent("MouseEvent");
//initMouseEvent(type, canBubble, cancelable, view, clickCount,
// screenX, screenY, clientX, clientY, ctrlKey,
// altKey, shiftKey, metaKey, button, relatedTarget);
simulatedEvent.initMouseEvent(type, true, true, event.view, 1,
first.screenX, first.screenY,
first.clientX, first.clientY, false,
false, false, false, /*left*/0, null);
first.target.dispatchEvent(simulatedEvent);
//event.preventDefault();
}
document.addEventListener("touchstart", touchHandler, true);
document.addEventListener("touchmove", touchHandler, true);
document.addEventListener("touchend", touchHandler, true);
document.addEventListener("touchcancel", touchHandler, true);
document.addEventListener("touchleave", touchHandler, true);
}(document));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment