Reputation: 75
if you press "R" the amount should go up by 1, but if you press "R" when the program is 'waiting' it should not detect the input. But with delay and sleep the program keeps track of the input and does the input after the delay. Is there any function to wait and not gaining any input?
import java.util.concurrent.TimeUnit;
int amount;
void setup()
{
size(200, 200);
}
void draw()
{
background(#FEF4E9);
fill(#FF0000);
if (key == 'r'){
//delay(500);
try{
Thread.sleep(500);
}catch(InterruptedException e){
System.out.println("got interrupted!");
}
amount++;
println("amount: "+amount);
}
}
Upvotes: 1
Views: 83
Reputation: 3137
Thread.sleep(500);
will simply block the thread for 500 milliseconds; it does not keep the thread from receiving OS messages such as keyboard input. The messages will simply be put on the message queue until the thread unblocks, at which point they will run. If you are trying ignore user input for a specific time period, you need to keep track of the time yourself and simply ignore input for that duration.
long ignoreTime = System.currentTimeMillis() + 500;
...
if (System.currentTimeMillis() > ignoreTime) { // only process after 500 timeout
doProcessingForR();
}
Upvotes: 1