# Key pressed variable

**URL:** https://discourse.processing.org/t/key-pressed-variable/20555
**Category:** Libraries
**Tags:** homework
**Created:** [May 6, 2020, 5:58pm UTC](https://discourse.processing.org/t/key-pressed-variable/20555 "2020-05-06T17:58:01Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![ploop1](https://avatars.discourse-cdn.com/v4/letter/p/3be4f8/32.png) [@ploop1](https://discourse.processing.org/u/ploop1)
#### Post date: [May 6, 2020, 5:58pm UTC](https://discourse.processing.org/t/key-pressed-variable/20555/1 "2020-05-06T17:58:01Z")

</div>

Hi, I am a student and struggling to figure out what is wrong with my programme. It is simply to play/pause a video at the press of the space bar however it doesn’t seem to continue not playing when the spacebar is released?  
Here is the code:

```auto
import processing.video.*; //
Movie myVideo; // 

void setup() {
   
  ;
  myVideo = new Movie(this, "transit.mov");
  surface.setResizable(true);
  surface.setSize(640,360);
  background(0);
  myVideo.loop();
    
}

//this function selects which video to read ( in this case there's only one)
void movieEvent(Movie m) { 
  myVideo.read();  
}

void draw() {    
  image(myVideo, 0, 0); //
  
  if(keyPressed == false){
  if (key == ' '|| key == ' '){
    myVideo.play(); //
    }
     }
    else if (keyPressed == true){
    if (key == ' '|| key == ' '){
    myVideo.pause();
    }
    }
}

If anyone could help that would be amazing thanks!
```

---

<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: [May 6, 2020, 6:01pm UTC](https://discourse.processing.org/t/key-pressed-variable/20555/2 "2020-05-06T18:01:23Z")

</div>

please use keyPressed as a function  
[https://www.processing.org/reference/keyPressed\_.html](https://www.processing.org/reference/keyPressed_.html)  
and

[https://www.processing.org/reference/keyReleased\_.html](https://www.processing.org/reference/keyReleased_.html)

---

<div class="post-metadata">

### Author: ![ploop1](https://avatars.discourse-cdn.com/v4/letter/p/3be4f8/32.png) [@ploop1](https://discourse.processing.org/u/ploop1)
#### Post date: [May 6, 2020, 6:08pm UTC](https://discourse.processing.org/t/key-pressed-variable/20555/3 "2020-05-06T18:08:49Z")

</div>

Hi, This seems to make no difference?

---

<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: [May 6, 2020, 6:14pm UTC](https://discourse.processing.org/t/key-pressed-variable/20555/4 "2020-05-06T18:14:59Z")

</div>

Hello!

Welcome to the forum!

Great to have you here.

Do you want the video to stop as long as you hold down the Space Bar?

Or click Space Bar briefly and switch between play and pause?

---

<div class="post-metadata">

### Author: ![ploop1](https://avatars.discourse-cdn.com/v4/letter/p/3be4f8/32.png) [@ploop1](https://discourse.processing.org/u/ploop1)
#### Post date: [May 6, 2020, 6:16pm UTC](https://discourse.processing.org/t/key-pressed-variable/20555/5 "2020-05-06T18:16:45Z")

</div>

Hello!

I wanted the video to stop when the spacebar is pressed and then play again at the press of the spacebar, similar to that of youtube.

Thanks

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [May 6, 2020, 8:04pm UTC](https://discourse.processing.org/t/key-pressed-variable/20555/6 "2020-05-06T20:04:56Z")

</div>

Hello,

> [@ploop1](#):
>
> I wanted the video to stop when the spacebar is pressed and then play again at the press of the spacebar

You can adapt this example for your needs:

```auto

boolean toggle = false;

void setup()
  {
  println(toggle);
  println("Click on sketch window with mouse to make it active!");
  println("Hit a key to toggle background colour!");
  println(toggle);
  }

void draw() 
  {    
  }

void keyPressed()
  {
  toggle = !toggle;
  if (toggle)
    {
    background(255, 0, 0);
    println(toggle);
    }
  else 
    {
    background(0, 255, 0);
    println(toggle);
    }
  }

```

Understand what it is doing before using it.

References:

> **[boolean / Reference](https://processing.org/reference/boolean.html)**
>
> Datatype for the Boolean values true and false. It is common to use boolean values with control statements to determine the flow of a program. The first time a variable is written…

> **[keyPressed() / Reference](https://processing.org/reference/keyPressed_.html)**
>
> The keyPressed() function is called once every time a key is pressed. The key that was pressed is stored in the key variable. For non-ASCII keys, use the keyCode varia…

`:)`

---

<div class="post-metadata">

### Author: ![ploop1](https://avatars.discourse-cdn.com/v4/letter/p/3be4f8/32.png) [@ploop1](https://discourse.processing.org/u/ploop1)
#### Post date: [May 6, 2020, 8:43pm UTC](https://discourse.processing.org/t/key-pressed-variable/20555/7 "2020-05-06T20:43:37Z")

</div>

Hi,

Thanks alot for the help. I’ll be sure to check it out tomorrow!

---

<div class="post-metadata">

### Author: ![ploop1](https://avatars.discourse-cdn.com/v4/letter/p/3be4f8/32.png) [@ploop1](https://discourse.processing.org/u/ploop1)
#### Post date: [May 7, 2020, 11:52am UTC](https://discourse.processing.org/t/key-pressed-variable/20555/8 "2020-05-07T11:52:42Z")

</div>

Worked a charm!

Thank you.
