Key pressed variable

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:

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!
1 Like

please use keyPressed as a function
https://www.processing.org/reference/keyPressed_.html
and

https://www.processing.org/reference/keyReleased_.html

1 Like

Hi, This seems to make no difference?

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?

1 Like

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

1 Like

Hello,

You can adapt this example for your needs:


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:

:)

2 Likes

Hi,

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

1 Like

Worked a charm!

Thank you.

2 Likes