I’ve been trying to make a program that counts how long you’ve pressed a key. Is this possible in Processing Java?
Here’s my code so far:
int frames;
void keyPressed() {
frames++;
}
void keyReleased() {
println(frames);
frames = 0;
}
I’ve been trying to make a program that counts how long you’ve pressed a key. Is this possible in Processing Java?
Here’s my code so far:
int frames;
void keyPressed() {
frames++;
}
void keyReleased() {
println(frames);
frames = 0;
}
You need an active sketch, not a static one which is as simple as adding a draw function
int frames;
void draw(){
}
void keyPressed() {
frames++;
}
void keyReleased() {
println(frames);
frames = 0;
}
Hi
Welcome to our community you need kind of timer
Heres a quick example, used a boolean to only detect the key pressed once, that way you get the exact seconds its held down, millis is a reference to milliseconds, hence the division in the print statement
float millis;
boolean pressed;
void draw() {
}
void keyPressed() {
if (pressed == false) {
millis = millis();
pressed = true;
}
}
void keyReleased() {
println((millis()-millis)/1000);
pressed = false;
}
And here is other example
int d = 0;
void setup() {
size(640, 320);
}
void draw() {
}
void keyPressed() {
if (key == 'j') {
d = millis();
}
}
void keyReleased() {
if (key == 'j') {
d = millis() - d/1000;
println("j key pressed " + d + " milliseconds");
}
}
Or this
int d = 0;
void setup() {
size(640, 320);
}
void draw() {
}
void keyPressed() {
d = millis();
}
void keyReleased() {
d = millis() - d;
println(" key pressed " + d + " milliseconds");
}
That way doesnt work, was my initial try aswell, keyPressed keeps overriding the variable d, thats why i used a boolean to only set d once
Hi
I didn’t test mine because I am using my tablet
Its alright, my initial thought was, keyPressed would only detect once per press aswell, turns out no, it detects once per frame which is odd, a boolean always comes in handy for things like that though
Yes you are true Sir
Maybe you can do it that way …
int d = -1;
int dd = 0;
void setup() {
size(640, 320);
}
void draw() {
background(0);
if (keyPressed) {
if (d==-1) {
d = millis();
}
else {
dd = millis() - d;
}
}
else {
d=-1;
}
fill(255);
text(dd + "ms",width/2,height/2);
}
Cheers
— mnse