Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect long press on WinJS / Metro app

Is there a built-in for long press event for metro apps? Possibly without using any external libraries such as jQuery.

like image 610
Stefania Avatar asked Nov 19 '25 05:11

Stefania


1 Answers

Do you mean the tap-and-hold touch gesture? If so, the event is MSGestureHold, but you have to configure an MSGesture object to pick these up:

var gestureObject = new MSGesture();
gestureObject.target = divElement;
divElement.gestureObject = gestureObject;
divElement.addEventListener("pointerdown", pointerDown);
divElement.addEventListener("MSGestureHold", gestureHold);

function pointerDown(e) {
    //Associate this pointer with the target's gesture
    e.target.gestureObject.addPointer(e.pointerId);
}

function gestureHold(e) {
    //Do what you need.
}

You use this same structure for MSGestureTap events as well as MSGestureStart, MSGestureChange, MSGestureEnd, and MSInertiaStart for other touch interactions. That is, having creating the MSGesture event and handled pointerdown to associate the pointer with the gesture, you can just add listeners for the other MSGesture* events as shown above for MSGestureHold.

I have a basic piece of code for this in the companion content for Chapter 12 of my second edition preview book (free from MSPress). It's a sample called PointerEvents.

like image 171
Kraig Brockschmidt - MSFT Avatar answered Nov 22 '25 01:11

Kraig Brockschmidt - MSFT



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!