Derek 朕會功夫
Derek 朕會功夫

Reputation: 94319

How to Create a TouchEvent in Chrome?

The W3C specification declares initTouchEvent as following:

void initTouchEvent (in DOMString    type,
                     in boolean      canBubble,
                     in boolean      cancelable,
                     in AbstractView view,
                     in long         detail,
                     in boolean      ctrlKey,
                     in boolean      altKey,
                     in boolean      shiftKey,
                     in boolean      metaKey,
                     in TouchList    touches,
                     in TouchList    targetTouches,
                     in TouchList    changedTouches);

However, when I try it in Chrome 44:

var e = document.createEvent('TouchEvent');
e.initTouchEvent("touchstart", true, true, window, 1,
                 false, false, false, false, touches, null, null);

where touches is a valid TouchList, this is what Chrome created:

> TouchEvent {}
    altKey: false
    bubbles: true
    cancelBubble: false
    cancelable: true
    changedTouches: null
    charCode: 0
    ctrlKey: true
    currentTarget: null
    defaultPrevented: false
    detail: 0
    eventPhase: 0
    keyCode: 0
    layerX: 0
    layerY: 0
    metaKey: false
    pageX: 0
    pageY: 0
    path: Array[0]
    returnValue: true
    shiftKey: false
    srcElement: null
    target: null
    targetTouches: null
    timeStamp: 1435339572699
    touches: null
    type: "[object Window]"
    view: null
    which: 0

Look closely at the type field. Seems like Chrome is not following the spec where the 4th parameter turned into the type instead of the 1st parameter.

This brings us the question, that how do I actually create a TouchEvent in Chrome since it does not follow the spec?

Upvotes: 4

Views: 2803

Answers (1)

Derek 朕會功夫
Derek 朕會功夫

Reputation: 94319

By looking at the Chromium source and Qiita (in Japanese), this seems like how its parameters are arranged:

initTouchEvent (TouchList touches,
                TouchList targetTouches,
                TouchList changedTouches,
                String type,
                Window view,
                number screenX,
                number screenY,
                number clientX,
                number clientY,
                boolean ctrlKey, 
                boolean altKey,
                boolean shiftKey,
                boolean metaKey);

Notice Chrome does not follow the W3C spec.


Relevant part in the Chromium source:

TouchEvent.cpp Line 63:

void TouchEvent::initTouchEvent(ScriptState* scriptState, TouchList* touches, TouchList* targetTouches,
        TouchList* changedTouches, const AtomicString& type,
        PassRefPtrWillBeRawPtr<AbstractView> view,
        int, int, int, int,
        bool ctrlKey, bool altKey, bool shiftKey, bool metaKey)
{
    if (dispatched())
        return;

    if (scriptState->world().isIsolatedWorld())
        UIEventWithKeyState::didCreateEventInIsolatedWorld(ctrlKey, altKey, shiftKey, metaKey);

    bool cancelable = true;
    if (type == EventTypeNames::touchcancel)
        cancelable = false;

    initUIEvent(type, true, cancelable, view, 0);

    m_touches = touches;
    m_targetTouches = targetTouches;
    m_changedTouches = changedTouches;
    m_ctrlKey = ctrlKey;
    m_altKey = altKey;
    m_shiftKey = shiftKey;
    m_metaKey = metaKey;
}

Upvotes: 2

Related Questions