We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › double click problem
Page Index Toggle Pages: 1
double click problem (Read 2009 times)
double click problem
Feb 14th, 2007, 1:53pm
 
Hello there,
I'm very very new to processing and any form of programming language. I hope I'm posting this in the right forum Undecided. I would need help on something I've been trying to figure out for two days.

My program is simple. All it does is register is the user single clicked the screen or double clicked. When it's a single click, a "1" is displayed on the screen and if it's a double click, a "2" is displayed. The problem resides in the double click: the first click is -always- registered as a single click. (so it always shows a "1" and then the "2").

Here is the code
Code:

//global variables

float lastClick = millis(); //the moment in milliseconds of the last click

//END global variables

void setup(){
size(400,400);
background(100);
smooth();

}

void drawText(int xText, int yText, boolean textNumber){
// this piece of code was taken on processing.org
// Load the font. Fonts are located within the
// main Processing directory/folder and they
// must be placed within the data directory
// of your sketch for them to load
// Set the font and its size

PFont fontA = loadFont("Ziggurat-HTF-Black-32.vlw");
textFont(fontA, 30);

//clears background after every click
background(100);

//Create a condition so that the text would be "1" or "2"
//depending if it's a single click or a double click
if(textNumber == true){
//double click
fill(0,0,0);
text("2", xText, yText);
}
else{
//single click
fill(255,255,255);
text("1", xText, yText);
}
}

boolean doubleClick(){
//the moment in millisecond() of the current click
float currentClick = millis();

//the difference between the two clicks
float clickDifference = currentClick - lastClick;

//set lastClick equal to currentClick
lastClick = currentClick;

//maximum amount of time in milliseconds to be counted as a double-click
float doubleClickSpeed = 200;

if(clickDifference < doubleClickSpeed){
return true;
}
else{
return false;
}

}

void mousePressed(){

if(doubleClick()){
drawText(width/2, height/2, true);

}
else{
drawText(width/2, height/2, false);

}

}
void draw(){
}


Thank you Smiley
Re: double click problem
Reply #1 - Feb 14th, 2007, 2:06pm
 
You need to delay drawing a 1 after a single click, to find out wether there's another. As it standas on the first click it'll show a 1 because the second click hasn't happened yet. Processing has no way to look into the future to see if there'll be a second click or not, so you need to wait before declaring it a single or double.
Re: double click problem
Reply #2 - Feb 14th, 2007, 3:22pm
 
Ah! Thank you JohnG! I tried to integrate a sort of counter  in my program so that it would only start counting at 1 click... but it's giving me an error now o_o

Semantic Error: The method "boolean doubleClick();" must contain a return statement with an expression compatible with type "boolean"

Code:


boolean doubleClick(){
//the moment in millisecond() of the current click
float currentClick = millis();//as to be a float cause it's in milliseconds
//the difference between the two clicks
float clickDifference = currentClick - lastClick;
//set lastClick equal to currentClick
lastClick = currentClick;
//maximum amount of time in milliseconds to be counted as a double-click
float doubleClickSpeed = 200;

if((counter < 0)&&(clickDifference < doubleClickSpeed)){
return true;
}
else if ((counter < 0)&&(clickDifference > doubleClickSpeed)){
return false;
}
else{
counter =0;}

}

void mousePressed(){

counter ++;
if(doubleClick()){
drawText(width/2, height/2, true);

}else{
drawText(width/2, height/2, false);

}

Re: double click problem
Reply #3 - Feb 14th, 2007, 3:59pm
 
there's a much simpler method:

Code:

void mouseClicked() {
int count = mouseEvent.getClickCount();
println(count); // count is 2 for double click
}
Re: double click problem
Reply #4 - Feb 14th, 2007, 4:26pm
 
Me like simpler Cheesy. Thank you fry. I've tried replacing my mousePressed() for your mouseClicked()and tested it but it still registers 1 and 2 when I double click. I'm all confused lol
Re: double click problem
Reply #5 - Feb 19th, 2007, 3:28pm
 
hello,

here is my approach. maybe it will help you a little.

Quote:


/**
if you do a doubleclick action within the time of the variable clickspeed a white rect will appear
*/

int counter=0;
float containerone;
float containertwo;
float clicktime;
float clickspeed = 200.0;

void setup(){
 size(200,200);
 background(0);
}

void draw(){
}

void mousePressed(){
 counter++;
 int switchvar=counter%2; // converts the var counter to 0 if counter is even or to 1 if counter is uneven (checkout modulo 2!!)
 float clicktime = millis();
 switch (switchvar) { // saves the clicktime alternatly to different variables (containerone and containertwo)
 case 0:
   containerone = clicktime;
   break;
 case 1:
   containertwo = clicktime;
   break;
 }
 if (abs(containertwo - containerone) < clickspeed){ // checks if you clicked within the clickspeed
   fill(255);
   rect(50,50,50,50);
 }
 else {
   background (0);
 }
}


i am fairly new to this cool open source language.
i would like to have the doubleclick also simpler but i don´t understand what fry has done. seems that the mouseEvent variable isn't documented in the reference(s. edit).

greetings,
slex

edit: found this:
http://www.processinghacks.com/hacks/doubleclick
Re: double click problem
Reply #6 - Feb 22nd, 2007, 10:26pm
 
Thank you slex! I've been working on this problem for a couple of days and got to sit down with a friend that knows Java. And I have no idea how I managed to figure my problem out. But the program works now (yay! I'm so happy).

Thank you also for that website link. I think I'll be looking into that processinghack site to see if I can learn some stuff from it Smiley
Re: double click problem
Reply #7 - Feb 26th, 2007, 12:34am
 
Edited:

the code in this post does not work, but i left it here for a paper trail to my final solution. check like 2 posts farther down.


here is the "hacked" processing code for the click events. i am assuming they call it "hacked" because it uses some java awt stuff not wrapped by Processing?

Code:
void mousePressed(MouseEvent e) {
 if (e.getClickCount()==2)
   println("<double click>");
 
 if (e.getButton()==MouseEvent.BUTTON3)
   println("<right button>");

 // this prints out the event in descriptive form
 println(e);
}


anyhow.. you will still need to implement a timer if you wanted to check for both single AND double clicks. you could do this relatively easily with the following code:

Code:
int clicks = 0;
float clicktime = 0.0;
float clickspeed = 1000.0;

void draw()
{
 if(clicktime > clickspeed){
   println("<single click>");
   clicktime = 0.0;
 }
}

void mousePressed(MouseEvent e)
{
 if (e.getClickCount()==1 && e.getButton()==MouseEvent.BUTTON1)
   clicktime = millis();
 
 if (e.getClickCount()==2 && e.getButton()==MouseEvent.BUTTON1){
   println("<double click>");
   clicktime = 0.0;
 }
 
 if (e.getClickCount()==1 && e.getButton()==MouseEvent.BUTTON3){
   println("<right button>");
 }
 
 // this prints out the event in descriptive form
 println(e);
}


it could be easily modified to detect double right clicks, triple (quadruple, etc.) clicks, adding a mouseReleased() method for simultaneous clicking (maybe clicking left button while right button is held, etc.)..

hope this helps. it's basically the same as the other example listed in this post, but it uses a "built-in" counter rather than implementing your own counter and using modulus, etc..

Edited:

also.. you could probably use your if statements in mousePressed() to assign a value to a variable, like "buttonPressed", that could be uniformly implemented in the draw() if-block that determines if you've beaten clickspeed. this way, you could switch(buttonPressed) in your draw() method to determine which mouse button to respond to.
Re: double click problem
Reply #8 - Feb 26th, 2007, 12:45am
 
yargh. my above code produces a few strange anomalies. i had it working properly earlier .. i'll try to funk around with it and get it operational again.

you get the idea, though..

Edited:

the timing check is off, because of my haphazard implementation of millis(). i'm working on it now.
Re: double click problem
Reply #9 - Feb 26th, 2007, 1:03am
 
ok. this code is sloppy (though rather easily cleanable) but it works, and it works every time.

Code:
int clicks = 0;
float clicktime = 0.0;
float clickspeed = 500.0;
boolean clicked = false;

void draw()
{
 if(millis() > clickspeed + clicktime && clicked){
   clicked = false;
   println("<single click>");
   clicktime = 0.0;
 }
}

void mousePressed(MouseEvent e)
{
 clicked = false;

 if (e.getClickCount()==1 && e.getButton()==MouseEvent.BUTTON1){
   clicked = true;  
   clicktime = millis();
 }
 
 if (e.getClickCount()==2 && e.getButton()==MouseEvent.BUTTON1){
   println("<double click>");
   clicktime = 0.0;
 }
 
 if (e.getClickCount()==1 && e.getButton()==MouseEvent.BUTTON3){
   println("<right button>");
 }
}


at this point, the variable clicked is only used to determine if a single click has been issued or not.
Re: double click problem
Reply #10 - Feb 26th, 2007, 2:40pm
 
that's still needlessly complicated. if you're trying to discern between single and double clicks then yes, you'll have to test your single clicks as to whether they don't turn into a double.

1) don't override mouseEvent unless you absolutely have to, because you lose queueing of events to a safe point in draw. this means that if your mouseEvent() function tries to draw, it could happen at a bad time for your app.

2) use "if (mouseButton == RIGHT)" to check for the right mouse button. this also handles ctrl-click on the mac. again, this is voided if you override the awt version of mouseEvent.
http://processing.org/reference/mouseButton.html

haliphax, the help is appreciated, but please do more reading about how processing works; you're clearly familiar with java, but these are the sort of problems with java that processing is designed to avoid.

modified version:

Code:
int clickTime = 0;
int clickSpeed = 200;
boolean clicked = false;

void draw() {
if (clicked && (millis() > clickTime + clickSpeed)) {
clicked = false;
println("<single click>");
clickTime = 0;
}
}

void mouseClicked() {
clicked = false;

if (mouseEvent.getClickCount() == 1) {
clicked = true;
clickTime = millis();

} else if (mouseEvent.getClickCount() == 2) {
println("<double click>");
clickTime = 0;
}

// not sure why this is in here, necessarily
if (mouseButton == RIGHT) {
println("<right button>");
}
}
Re: double click problem
Reply #11 - Feb 26th, 2007, 7:36pm
 
nah.. i think the problem is that i am NOT familiar with java, and i'm mixing the two of them earlier than i should. Smiley i will make sure to further research the processing libraries/functions in question before replying in the future.

edit: also, the <right button> stuff was in there just as an example.

in retrospect, i believe the "hacked" code i was using as my reference must have been written before processing reached the point of such pristine wrapping of the mouse events (no sarcasm--it's beautiful).
Re: double click problem
Reply #12 - Feb 26th, 2007, 9:17pm
 
Hi.

Out of curiosity, could you please explain what is being checked for over here?

if (clicked && (millis() > clickTime + clickSpeed)) {

In the '(millis() > clickTime + clickSpeed))' part, to be specific.

From what i understand, it's checking to see if

(the amount of milliseconds that have passed since the program began running)

is greater than

(the amount of milliseconds that have passed up until the last single-click)

plus

(the amount of milliseconds that are allowed to pass between clicks for them to be considered a double-click).



...ohhhhhhhhhh.

i just got it.

because if the time that the program has been running is longer than the time of the last click, plus 200 milliseconds, that means no second click happened within that 200 millisecond window!

...right?
Re: double click problem
Reply #13 - Mar 1st, 2007, 8:59pm
 
right.
Page Index Toggle Pages: 1