Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

A Simple Way to Prevent Unintended Double Taps in MR Apps

DZone's Guide to

A Simple Way to Prevent Unintended Double Taps in MR Apps

Want to make sure users don't 'double-click' in your MR apps? Here's a simple helper class you can implement and an example you can check out.

· IoT Zone ·
Free Resource

This is an easy and simple tip, but one that I have used in almost every Mixed Reality app at one point. It’s mostly applicable to HoloLens: Users, especially new users, tend to double (triple, or even more) tap when they are operating an app because the gestures are new. This can lead to undesirable and confusing results, especially with toggles. So it can help a little to ‘dampen out’ those double clicks.

I created this very simple helper class called DoubleClickPreventer:

using UnityEngine;

namespace HoloToolkitExtensions.Utilities
{
    public class DoubleClickPreventer
    {
        private readonly float _clickTimeOut;

        private float _lastClick;

        public DoubleClickPreventer(float clickTimeOut = 0.1f)
        {
            _clickTimeOut = clickTimeOut;
        }

        public bool CanClick()
        {
            if (!(Time.time - _lastClick < _clickTimeOut))
            {
                return false;
            }
            _lastClick = Time.time;
            return true;
        }
    }
}


Basically, every time you ask it if you can click, it checks if a set amount of time (default 0.1 second) has passed since the last click. Its usage is pretty simple: Just make a behavior that implements IInputClickHandler (from the Mixed Reality Toolkit) as usual, to define a DoubleClickPreventer member, create it in Start like this:

_doubleClickPreventer = new DoubleClickPreventer(0.5f);


And then, in your OnInputClicked implementation, use something like this:

if (_doubleClickPreventer.CanClick())
{
    // do stuff
}


And this will prevent a second click happening more than once every half second.

I made a little demo project in which one cube has a GuardedClicker behavior that implements the DoubleClickPreventer, and the other has a SimpleClickPreventer that just registers every click.

image

If you click like a maniac for about a second or two on both cubes, you will clearly see a different result.

Note: The InputClickedEventData that you get in OnInputClicked.IInputClickHandler contains a “TapCount” property, but I have found that’s usually just 1, and that does not stop the ‘stuttery double tap’ we are trying to prevent here. Also, this solution allows for fast clicks on separate objects, but not fast clicks on the same object.

The demo project (although it’s very little) can be downloaded here.

Topics:
iot ,mixed reality app development ,double-click ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}