Hammer.js: Use Hammer for touch ONLY, not mouse

Created on 18 Nov 2016  路  4Comments  路  Source: hammerjs/hammer.js

I don't want Hammer doing anything with my mouse events. They're handled separately. No, I don't want to call preventDefault or stopPropagation() on them - I want it to get completely out of the way!

How do I set up hammer to do this?

Note I'm really not interested in writing an special handling to dance around Hammer (I included Hammer in the first place to get AWAY from kludge-y event juggling). It should just mind its own business and leave my mouse events alone, and there should be an option for this.

Most helpful comment

Thanks. Yeah I ended up doing something like that (actually checking e.srcEvent instanceof MouseEvent - you'll find that 'MouseEvent' in window even on mobile. Correct me if I'm wrong, but this is the case on iOS and Android).

Still it would be make it clearer if this was part of the API, maybe initializing a recognizer with e.g.

new Hammer.Tap({
    event: "doubletap",
    taps: 2,
    pointerType: Hammer.POINTER_TOUCH
});

with the option to instead pass POINTER_MOUSE or POINTER_ALL, or whatever other pointers come along (POINTER_DUCK_HUNT_LIGHT_GUN?)

All 4 comments

if ('ontouchstart' in window) {
  // do what ever you want with hammer?
}

Doesn't work.

You have a touchscreen laptop (now very common. Windows 10 is designed around it!). It can do both touch and click. Hammer is still interfering with clicks, making them do unwanted things. This doesn't solve the problem.

What I found easiest was to ignore mouse events by checking the pointerType before processing within the Hammer event handler and allowing mouse events to propagate.

For example

function (event) {
    if (event.pointerType === 'touch') {
        // handle the touch event
    }
}

Edit: It looks like you may need to monitor issue #547. I have not noticed a problem but now I will look more closely.

Thanks. Yeah I ended up doing something like that (actually checking e.srcEvent instanceof MouseEvent - you'll find that 'MouseEvent' in window even on mobile. Correct me if I'm wrong, but this is the case on iOS and Android).

Still it would be make it clearer if this was part of the API, maybe initializing a recognizer with e.g.

new Hammer.Tap({
    event: "doubletap",
    taps: 2,
    pointerType: Hammer.POINTER_TOUCH
});

with the option to instead pass POINTER_MOUSE or POINTER_ALL, or whatever other pointers come along (POINTER_DUCK_HUNT_LIGHT_GUN?)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  路  10Comments

julmot picture julmot  路  9Comments

priyankaviswan picture priyankaviswan  路  5Comments

elmerbulthuis picture elmerbulthuis  路  3Comments

terion-name picture terion-name  路  10Comments