How to make keyPressed respond to the number of times the key is pressed

edited May 2016 in How To...

I'm very new to Processing. I am working on a project that involves Makey Makey. I was wondering if it is possible to use keyPressed or something similar to respond to the number of times the key is pressed? For instance, I want the user to press LEFT three times and RIGHT two times, and after that the video will play. Is there a way to do it?

Answers

  • edited May 2016 Answer ✓
    /**
     * Makey Movie Secret Key Combo (v1.0.1)
     * GoToLoop (2016-May-07)
     *
     * forum.Processing.org/two/discussion/16476/
     * how-to-make-keypressed-respond-to-the-number-of-times-the-key-is-pressed
     */
    
    import processing.video.Movie;
    
    static final String FILENAME = "video.mp4";
    static final int FPS = 30, INTERVAL = 1 * FPS, DELAY = 5, SKIP = 10;
    static final int LEFTS = 3, RIGHTS = 2;
    
    int lefts, rights;
    Movie mov;
    
    void setup() {
      size(800, 600);
      frameRate(FPS);
      loadVideo(FILENAME);
    }
    
    void draw() {
      final boolean isPlaying = mov.hasBufferSink();
    
      frame.setTitle("Lefts: " + lefts + "   Rights: " + rights
        + (isPlaying? "   Time: " + nf(mov.time(), 3, 2)
        + "   Duration: " + nf(mov.duration(), 3, 2) : ""));
    
      if (isPlaying)  background(mov);
      else if (frameCount % INTERVAL == 0)  background((color) random(#000000));
    }
    
    void keyPressed() {
      final int k = keyCode;
    
      if (lefts != LEFTS)  lefts = k == LEFT? lefts + 1 : 0;
      else  rights = k == RIGHT & lefts == LEFTS &
        rights < RIGHTS? rights + 1 : (lefts = 0);
    
      if (rights == RIGHTS)  mov.play();
    
      if (k == ' ')  mov.pause();
      else if (k == ENTER | k == RETURN)  mov.jump(0);
      else if (k == 'A')  mov.jump(mov.time() - SKIP);
      else if (k == 'D')  mov.jump(mov.time() + SKIP);
    }
    
    void movieEvent(final Movie m) {
      m.read();
    }
    
    void loadVideo(final String filename) {
      mov = new Movie(this, filename) {
        @ Override public boolean hasBufferSink() {
          return playing;
        }
    
        @ Override protected void eosEvent() {
          super.eosEvent();
          if (!playing)  stop();
        }
      };
    
      mov.play();
      while (mov.width == 0)  delay(DELAY);
      mov.stop();
    
      if (mov.width > 0)  getSurface().setSize(mov.width, mov.height);
      //if (mov.width > 0)  size(mov.width, mov.height);
    }
    
  • Thank you so much!!!

    However, when I'm trying to run your code, I am getting "The function getSurface() does not exist"

  • edited May 2016 Answer ✓

    Grab P3: https://Processing.org/download/
    Being very careful to choose another path for your P3's "Sketchbook" when 1st running it!!! L-)

    If you wish for it to work w/ P2's previous versions, replace:
    if (mov.width > 0) getSurface().setSize(mov.width, mov.height);

    w/: if (mov.width > 0) size(mov.width, mov.height);.

    And also replace all getSurface(); w/ frame.

  • Thank you, you are a lifesaver!

  • Hey, sorry to bother you again. I was trying to combine your code with mine. I need those videos to play one after another, but for some reason I only get the sound without image from the second one and because of that 3 to 5 don't play as well.

    import processing.video.*;
    Movie myMovie1, myMovie2, myMovie3, myMovie4, myMovie5;
    boolean playMovie1=true;
    boolean playMovie2=false;
    boolean playMovie3=false;
    boolean playMovie4=false;
    boolean playMovie5=false;
    static final int FPS = 30, INTERVAL = 1 * FPS;
    static final int LEFTS = 3, RIGHTS = 2; 
     int lefts, rights;
    
    void setup()
    {
      size (displayWidth, displayHeight);
      background (0);
      frameRate(FPS); 
    
     myMovie1 = new Movie(this, "1.mp4");
     myMovie2 = new Movie(this, "2.mp4");
     myMovie3 = new Movie(this, "3.mp4");
     myMovie4 = new Movie (this, "4.mp4");
     myMovie5 = new Movie (this, "5.mp4"); 
    }
    
    void draw(){
    background(0);
    
    if(playMovie1==true){
    
    myMovie1.play();
    image(myMovie1,0, 0);
    if(myMovie1.time()>=myMovie1.duration()){
    myMovie1.stop();
    playMovie1=false;
    
    playMovie2=true;
    }
    }
    if (playMovie2==true) {final boolean isPlaying = myMovie2.hasBufferSink();
    
      frame.setTitle("Lefts: " + lefts + "   Rights: " + rights
        + (isPlaying? "   Time: " + nf(myMovie2.time(), 3, 2)
        + "   Duration: " + nf(myMovie2.duration(), 3, 2) : ""));
    
      if (isPlaying)  background(myMovie2);
      else if (frameCount % INTERVAL == 0)  background(0); 
    }
    
    
    
    if(playMovie3==true){
    
    myMovie3.play();
    image(myMovie3,0,0);
    if(myMovie3.time()>=myMovie3.duration()){
    myMovie3.stop();
    playMovie3=false;
    
    playMovie4=true;
    }
    }
    if(playMovie5==true){
    
    myMovie5.play();
    image(myMovie5,0, 0);
    if(myMovie5.time()>=myMovie5.duration()){
    myMovie5.stop();
    playMovie5=false;
    
    }}}
    
    
    
    void movieEvent(Movie m){
    m.read();
    }
    
    
    
    //PRESS LEFTx3, RIGHTx2
    void keyPressed()
    
    {
    
      if (lefts != LEFTS)  lefts = keyCode == LEFT? lefts + 1 : 0;
      else  rights = keyCode == RIGHT & lefts == LEFTS & rights < RIGHTS? rights + 1 : (lefts = 0);
    
      if (rights == RIGHTS)  {
    
    if (playMovie2==true){
    
    myMovie2.play();
    image(myMovie2,0,0);
    if(myMovie2.time()>=myMovie2.duration()){
    myMovie2.stop();
    playMovie2=false;
    playMovie3=true;
    }}}
    
    
     //PRESS UP ONCE
     if (keyCode == UP)
     {
       if(playMovie4==true){
         myMovie4.read();
    myMovie4.play();
    image(myMovie4,0,0);
    if(myMovie4.time()>=myMovie4.duration()){
    myMovie4.stop();
    playMovie4=false;
    playMovie5=true;
    }}
    
     }
    }
    

    Is there a better way to do it?

  • W/o using arrays there's no sane way to tame your sketch at all: :-t
    https://Processing.org/reference/arrayaccess.html
    http://docs.Oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

    Try to adapt this Minim sketch to use Movie instead:
    https://forum.Processing.org/two/discussion/16413/minim-pause-does-not-work#Item_12

    Once you accomplish that, it's not far away to get the rest. :-bd

  • Hi again. I'm trying to adapt that Minim sketch as you advised, but I am somewhat confused, for I have never used Minim before. How would I go about changing "ddf.minim.Minim" and "ddf.minim.Playable" to something similar in the video library?

  • How would I go about changing "ddf.minim.Minim" and "ddf.minim.Playable" to something similar in the video library?

    Video library use import processing.video.Movie; only for playing videos.
    Obviously that Minim's style used there to load audios is different from Movie's.
    Which you already know how to do it. But I guess now you won't be able to pull that off. :|

  • For example, in the original, the AudioPlayer objects are created via Minim's loadFile() method:

    final Minim m = new Minim(this);
    final String[] paths = dataFile("").list(MP3_FILTER);
    final int len = paths.length;
    
    songs = new Playable[len];
    for (int i = 0; i < len; songs[i] = m.loadFile(paths[i++]));
    songs[0].play();
    
    printArray(paths);
    println();
    

    Movie class is directly instantiated via new instead:

    final String[] paths = dataFile("").list(VIDEO_FILTER);
    final int len = paths.length;
    
    movies = new Movie[len];
    for (int i = 0; i < len; movies[i] = new Movie(this, paths[i++]));
    movies[0].play();
    
    printArray(paths);
    println();
    
  • Dear GoToLoop, I try to use the video player you wrote when clicking/selecting 3d objects, but I don't get anywhere!?

  • edited June 2017

    ... when clicking/selecting 3d objects, ...

    There are no 3D objects in the sketch I've posted in this more-than-1-year forum thread. 8-|

  • Sorry for the misunderstanding GoToLoop I try to use the code you wrote in this old forum thread to read videos when I click on 3d objects in my sketch but I don't manage to make it work...

Sign In or Register to comment.