Trap for a Button or Hotspot Double-Tap?

Hello,

Do you know if it is possible to write a script that is activated when the user taps twice rapidly on a button or hotspot, similar to a double-click with a mouse on a computer?

Thank you

Here’s the way I’d do it (for better or worse - I’ve barely tested this and I’m not known for good code):

const my_button = symbol.nodes.my_button_node_thingy;

var first_tap_seen = false;    // this'll keep track of whether we've just seen a tap

function callback_func() {  
    if (first_tap_seen) {
        text_flag.text("single_tap"); // or call your single tap stuff here
        first_tap_seen = false;
    }
}

my_button.on("pointerdown", (e) => {
    
    if (!first_tap_seen) {

        // no tap has been recently, so note that a tap
        // *has* been seen, but we'd better wait and see
        // if another tap happens. Our callback function,
        // will be called after a brief delay to check.
        //
        // one of two things can happen. Either the 
        // delay passes without the user tapping again,
        // in which case the callback function can say
        // it was a single tap.
        //
        // Or the user may tap again before that delay runs out
        // in which case we can say: hey, that must have
        // been a double-tap.

        first_tap_seen = true;

        Z.after(250, callback_func); // 250ms - set it shorter
                                    // if you like

    } else {

        // a tap has already happened recently, so this must be
        // a double-tap

        text_flag.text("double-tapped"); // or call your double-tap stuff here
        first_tap_seen = false; 
            // note that the "single tap" callback function will 
            // still be called, but it won't do anything
            // as we've set that first_tap_seen flag to false.
    }
    
});
2 Likes