# Detect keypress while in a loop

**URL:** https://discourse.processing.org/t/detect-keypress-while-in-a-loop/32850
**Category:** Coding Questions
**Created:** [October 15, 2021, 4:33pm UTC](https://discourse.processing.org/t/detect-keypress-while-in-a-loop/32850 "2021-10-15T16:33:48Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![NumericPrime](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/numericprime/32/16318_2.png) [@NumericPrime](https://discourse.processing.org/u/NumericPrime)
#### Post date: [October 15, 2021, 4:33pm UTC](https://discourse.processing.org/t/detect-keypress-while-in-a-loop/32850/1 "2021-10-15T16:33:48Z")

</div>

I am making a program to make music. However the playfunction has the problem, that I can’t interrupt it. So I wanted to implement a key to abort the play function.

Here is a example to show the problem. It also has the things implemented, that I tried:

```auto
boolean abort=false;

void setup() {
  size(200, 200);
}
void doSomeThing() {
  for (int i=0; i<10; i++) if (!abort) {
    //here the note is played.
    println(i);
    delay(500);
    redraw();
    if(keyPressed) if (key=='a') abort=true;
  } else {
    println("Process was aborted");
  }
  abort=false;
}
void draw() {
    if(keyPressed) if (key=='a') abort=true;
}
void keyPressed() {
  if (key=='d') doSomeThing();
  if (key=='a') abort=true;

```

So how can I detect a key while in a loop?

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [October 15, 2021, 4:45pm UTC](https://discourse.processing.org/t/detect-keypress-while-in-a-loop/32850/2 "2021-10-15T16:45:35Z")

</div>

> [@NumericPrime](#):
>
> `doSomeThing()`

in this function :

```auto
    if(keyPressed) 
        if (key=='a') { 
            abort=true; 
            return 
        }

```

worth a shot

but really you should get rid of the for loop and call `doSomeThing` from draw() when abort==false; 10 times (with a counter to 10)

I think that key is better recognized this way

---

<div class="post-metadata">

### Author: ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)
#### Post date: [October 17, 2021, 6:00pm UTC](https://discourse.processing.org/t/detect-keypress-while-in-a-loop/32850/3 "2021-10-17T18:00:14Z")

</div>

If I understand correctly, I second Chrisir’s idea of merging `doSomeThing` into `draw` loop.

Nevertheless, you can also try to use a [`thread`](https://processing.org/reference/thread_.html)

```auto
boolean abort=false;

void setup() {
  size(200, 200);
}
void doSomeThing() {
  for (int i=0; i<10; i++) if (!abort) {
    //here the note is played.
    println(i);
    delay(500);
    redraw();
  } else {
    println("Process was aborted");
  }
  abort=false;
}
void draw() {
}
void keyPressed() {
  if (key=='d') thread("doSomeThing");
  if (key=='a') abort=true;
}

```

This actually runs, but weird thing can happen if you press `d` again before aborting the last loop. It is up to your design:

- when multiple loops are running, `a` will abort all the loops
- when multiple loops are running, `a` will abort only the last loop or
- prevent multiple loops to run
