jor
jor

Reputation: 2157

JavaScript: translate long tap events to right click events

How can one automatically translate long tap events to right click events? Since many touch devices like the iPad don't provide a way to do a right click on a website this would be very handy because a website's code doesn't need to be adjusted.

For example this code is designed for desktop browser having mouse support:

<html>
    <head><title>Long tap to right click test</title></head>
    <body>
        <img src="dummy.png" oncontextmenu="alert('Hi!'); return false;" width="20" height="20" />
    </body>
</html>

The goal is to translate a long tap event to the right click event without modifying the code. (Just loading some JavaScript, of course.)

If've seen that https://github.com/furf/jquery-ui-touch-punch/ does something similar for drag'n'drop support on jQuery widgets. However this plugin doesn't support the long tap.

Also http://code.google.com/p/jquery-ui-for-ipad-and-iphone/ does actually perform the desired translation but it brakes scrolling, thus making it useless for regular websites with the need of scroll support.

Any help is appreciated - thanks!

Upvotes: 4

Views: 8747

Answers (3)

eurobax
eurobax

Reputation: 3

All solutions not work in desktop browsers. You should also tune up 'click' handler behaviour, cause all 'longtap' events should also be followed by 'click' event.

In this case something code like this:

itemEl.click(function(event){

    if ($(this).data('itemlongtouch')){
        $(this).data('itemlongtouch', false);
    }else{
        //some work
    }
});

itemEl.longTap(function(event){
    $(this).data('itemlongtouch', true);
    //some work
});

Upvotes: 0

dfsq
dfsq

Reputation: 193301

You can write simple plugin to handle this type of events. Lets call it longTap event. Example:

$.fn.longTap = function(options) {

    options = $.extend({
        delay: 1000,
        onRelease: null
    }, options);

    var eventType = {
        mousedown: 'ontouchstart' in window ? 'touchstart' : 'mousedown',
        mouseup: 'ontouchend' in window ? 'touchend' : 'mouseup'
    };

    return this.each(function() {
        $(this).on(eventType.mousedown + '.longtap', function() {
            $(this).data('touchstart', +new Date);
        })
        .on(eventType.mouseup + '.longtap', function(e) {
            var now = +new Date,
                than = $(this).data('touchstart');
            now - than >= options.delay && options.onRelease && options.onRelease.call(this, e);
        });
    });
};

Obviously you want to change mousedown and mouseup to touchstart and touchend in case of iPad.

Usage: http://jsfiddle.net/dfsq/RZgxT/1/

Upvotes: 6

iappwebdev
iappwebdev

Reputation: 5910

You can use a timeout for that:

var timeoutLongTouch;
var $mydiv = $j('#myDiv');

// Listen to mousedown event
$mydiv.on('mousedown.LongTouch', function () {
    timeoutLongTouch = setTimeout(function () {
        $mydiv.trigger('contextmenu');
    }, 1000);
})
// Listen to mouseup event
.on('mouseup.LongTouch', function () {
    // Prevent long touch 
    clearTimeout(timeoutLongTouch);
});

Upvotes: 1

Related Questions