# How to count how long a key is pressed

**URL:** <https://discourse.processing.org/t/how-to-count-how-long-a-key-is-pressed/37375>\
**Category:** Beginners\
**Created:** [June 6, 2022, 11:08pm UTC](https://discourse.processing.org/t/how-to-count-how-long-a-key-is-pressed/37375 "2022-06-06T23:08:12Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![gdt1k](https://avatars.discourse-cdn.com/v4/letter/g/48db29/32.png) [@gdt1k](https://discourse.processing.org/u/gdt1k)\
**Post date:** [June 6, 2022, 11:08pm UTC](https://discourse.processing.org/t/how-to-count-how-long-a-key-is-pressed/37375/1 "2022-06-06T23:08:13Z")

</div>

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:

```auto
int frames;
void keyPressed() {
frames++;
}

void keyReleased() {
println(frames);
frames = 0;
}

```

---

<div class="post-metadata">

**Author:** ![Mikey83](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mikey83/32/17956_2.png) [@Mikey83](https://discourse.processing.org/u/Mikey83)\
**Post date:** [June 6, 2022, 11:12pm UTC](https://discourse.processing.org/t/how-to-count-how-long-a-key-is-pressed/37375/2 "2022-06-06T23:12:06Z")

</div>

You need an active sketch, not a static one which is as simple as adding a draw function

```auto
int frames;

void draw(){
}

void keyPressed() {
   frames++;
}

void keyReleased() {
   println(frames);
   frames = 0;
}

```

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [June 6, 2022, 11:26pm UTC](https://discourse.processing.org/t/how-to-count-how-long-a-key-is-pressed/37375/3 "2022-06-06T23:26:09Z")

</div>

Hi

Welcome to our community you need kind of timer

---

<div class="post-metadata">

**Author:** ![Mikey83](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mikey83/32/17956_2.png) [@Mikey83](https://discourse.processing.org/u/Mikey83)\
**Post date:** [June 6, 2022, 11:42pm UTC](https://discourse.processing.org/t/how-to-count-how-long-a-key-is-pressed/37375/4 "2022-06-06T23:42:08Z")

</div>

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

```auto
float millis;
boolean pressed;

void draw() {
}

void keyPressed() {
  if (pressed == false) {
    millis = millis();
    pressed = true;
  }
}

void keyReleased() {
  println((millis()-millis)/1000);
  pressed = false;
}

```

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [June 6, 2022, 11:46pm UTC](https://discourse.processing.org/t/how-to-count-how-long-a-key-is-pressed/37375/5 "2022-06-06T23:46:13Z")

</div>

And here is other example

```auto

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");
  }
}

```

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [June 7, 2022, 12:06am UTC](https://discourse.processing.org/t/how-to-count-how-long-a-key-is-pressed/37375/6 "2022-06-07T00:06:49Z")

</div>

Or this

```auto

int d = 0;

void setup() {
  size(640, 320);
}

void draw() {
}

void keyPressed() {

  d = millis();
}

void keyReleased() {
  d = millis() - d;
  println(" key pressed " + d + " milliseconds");
}

```

---

<div class="post-metadata">

**Author:** ![Mikey83](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mikey83/32/17956_2.png) [@Mikey83](https://discourse.processing.org/u/Mikey83)\
**Post date:** [June 7, 2022, 12:11am UTC](https://discourse.processing.org/t/how-to-count-how-long-a-key-is-pressed/37375/7 "2022-06-07T00:11:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [June 7, 2022, 12:14am UTC](https://discourse.processing.org/t/how-to-count-how-long-a-key-is-pressed/37375/8 "2022-06-07T00:14:33Z")

</div>

Hi  
I didn’t test mine because I am using my tablet

---

<div class="post-metadata">

**Author:** ![Mikey83](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mikey83/32/17956_2.png) [@Mikey83](https://discourse.processing.org/u/Mikey83)\
**Post date:** [June 7, 2022, 12:16am UTC](https://discourse.processing.org/t/how-to-count-how-long-a-key-is-pressed/37375/9 "2022-06-07T00:16:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [June 7, 2022, 12:21am UTC](https://discourse.processing.org/t/how-to-count-how-long-a-key-is-pressed/37375/10 "2022-06-07T00:21:35Z")

</div>

Yes you are true Sir

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [June 7, 2022, 9:37am UTC](https://discourse.processing.org/t/how-to-count-how-long-a-key-is-pressed/37375/11 "2022-06-07T09:37:25Z")

</div>

Maybe you can do it that way …

```auto
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
