- Home /
Double click mouse detection ?
Hello world !
I want to make a double-click function with js. But when I tried to make it, I got messed with Time.time :D. So can anyone help with that ?
Thanks in advance !
hello double click ...
timeBetweenDubClick = 0.20 (or whatever)
when a click happens ...
gapHere == Time.time - previousClickTime
if gapHere < timeBetweenDubClick .. that is a doubleclick
previousClickTime = Time.time
Answer by sparkzbarca · Oct 12, 2012 at 07:50 PM
double click really means 2 clicks within a narrow range of time
bool one_click = false;
bool timer_running;
float timer_for_double_click;
//this is how long in seconds to allow for a double click
float delay;
if(input.GetMouseButtonDown(0))
{
if(!one_click) // first click no previous clicks
{
one_click = true;
timer_for_double_click = time.time; // save the current time
// do one click things;
}
else
{
one_click = false; // found a double click, now reset
do double click things
}
}
if(one_click)
{
// if the time now is delay seconds more than when the first click started.
if((time. time - time_for_double_click) > delay
{
//basically if thats true its been too long and we want to reset so the next click is simply a single click and not a double click.
one_click = false;
}
hope that helps! dont forget to mark if answered.
if (!one_click)
{
timer_for_double_click = Time.time;
one_click = true;
}
else
{
if ((Time.time - timer_for_double_click) > delay)
{
timer_for_double_click = Time.time;
} else
{
//Do something here if double clicked
one_click = false;
}
}
Answer by aphenine · Aug 16, 2016 at 05:04 AM
If you stay with the EventSystem method, you don't need to check timings and do it manually, the event data generated by each pointer press will do it for you.
Step 1: Learn about the EventSystem and EventTriggers scripts:
https://www.youtube.com/watch?v=3NBYqPAA5Es (Lovely YouTube video)
Step 2: For non-UI elements, you can add a EventTrigger to them too, but you also need to add a Physics Raycaster to the camera you're using. Then the stuff outlined in the tutorial above will work for 3D objects (and 2D objects if a 2D game and the 2D Raycaster is used)
Step 3: Get involved with Pointer event data
If you use this system, and you set up a callback in the editor for a pointer click on GameObject, then you can't control things like which mouse button was pressed or (as per the original question) whether it's a single or double click. Your click function will get called whichever button is used, however many time you click.
However, the EventSystem and Input Modules collect all of that stuff by default for you, you just need to get at it. To access it, you just need to do some extra stuff:
http://answers.unity3d.com/answers/1229912/view.html
Hope that helps.
This should be the accepted answer, it is a lot better solution than a custom input detection with Input.GetButtonDown() (and it should actually be GetButtonUp()). Imagine you want to switch over to using controller, not to mention touch control!
Answer by cenk5355 · Feb 11, 2014 at 02:07 PM
this is tested and working,good luck:
private var lastClickTime:float=0;
var catchTime:float=.25;
function Update () {
if(Input.GetButtonDown("Fire1")){
if(Time.time-lastClickTime<catchTime){
//double click
print("done:"+(Time.time-lastClickTime).ToString());
}else{
//normal click
print("miss:"+(Time.time-lastClickTime).ToString());
}
lastClickTime=Time.time;
}
}
almost. if you double click, then keep clicking faster than the catchTime, it registers as a double click for every single successive click, rather than requiring a double click after double clicking.
Answer by DSobscure · Aug 26, 2016 at 04:48 AM
try to use Event.current.clickCount
such like
void OnGUI()
{
if(Event.current.isMouse && Event.current.button == 0 && Event.current.clickCount > 1)
{
Debug.Log(Event.current.clickCount);
}
}
Answer by Yaakov_Shahak · Jan 15, 2018 at 01:33 PM
Corutines can be very useful for this purpose:
private WaitForSeconds doubleClickTreashHold = new WaitForSeconds(1f);
private int clickCount;
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
OnPointerClick();
}
}
private void OnPointerClick()
{
clickCount++;
if (clickCount == 2)
{
print("double click!");
clickCount = 0;
}
else
{
StartCoroutine(TickDown());
}
}
private IEnumerator TickDown()
{
yield return doubleClickTreashHold;
if (clickCount > 0)
{
clickCount--;
}
}
I modified this slightly to use
//input mouse click
clickcount++
If(clickCount !=2)
{
StartCoroutine(TickDown());
//Do single click stuff
}
else
{
clickCount = 0;
print("We double clicking now!");
}
ins$$anonymous$$d of calling the method OnPointerClick(), just because it did mostly the same thing but I didn't need to rewrite larger blocks of code to impliment. Thank you for the starting point.