3

I'm building a simple web game with mouse and touch support. Based on the accepted answer to JavaScript mapping touch events to mouse events I copy-pasted their code (with a couple of modifications).

I'm mapping the first touch the user makes to a left-click event, and the second they make (with the first touch held down) to a right-click event. Like so:

function touchHandler(event){
var touches = event.changedTouches;
var first   = touches[0];
var type    = "";

switch(event.type){
    case "touchstart" : type = "mousedown"; break;
    case "touchmove"  : type = "mousemove"; break;
    case "touchend"   : type = "mouseup";   break;
    default:return;
}
var button = 0;
if(first.identifier >= 1)
    button = 2;
document.getElementById("foo").innerHTML = first.identifier; //my "debug statement"
var simulatedEvent = document.createEvent("MouseEvent");
simulatedEvent.initMouseEvent(type,true,true,window,1,
                              first.screenX,first.screenY,
                              first.clientX,first.clientY,false,
                              false,false,false,button,null);
first.target.dispatchEvent(simulatedEvent);
event.preventDefault(); }

Here's the issue: my "debug statement" (see the code) revealed that identifier was being correct in the emulator modes in chrome, as well as on my android phone and MS Surface. But on my iPad? 1862464270, and counting. Clearly, I do not have that many fingers.

This issue occurs running either Chrome or Safari, but only on my iPad. Everything works fine on the other systems I've used for testing.

Does anyone have an idea of what might be causing this issue? Has anyone else encountered strange event.identifier behavior?

2

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.