Need to Minimize PApplet for keyPressed to Work

I'm having trouble using multiple keyPressed functions. It will work, but only after minimizing the PApplet window. Does anyone have any idea why this is? This seems like a java problem, but I really have no clue. I have the problem on two mac OS Xs, an older 10.9.2 and a newer 10.9.4 Mavericks. The Processing revision number is 2.2.1. Here is an example (pressing 'e' will not produce results after pressing 'p' until after the papplet window is minimized):

    void setup() {
     size(525, 680);
    }

    void draw() {
       background(0);
      rect(50, 50, 400, 400);
    }

    void keyPressed() {
     if (key == 'p'){         //pixelates a large portion of the screen
     loadPixels();
     for (int i = 100000; i < pixels.length; i++) {
     float rand = random(255);
     color a = color(rand, 110, 200, 100);
     pixels[i] = a;
    }
     updatePixels();  
     noLoop(); 
    }
    if (key == 'e') {   //switches two parts of the image
    PImage p = get();
    copy(p, 0, 0, 100, 400, 0, 280, 100, 400);
    copy(p, 0, 400, 100, 280, 0, 0, 100, 280);  
    }
    } 

Thank you for any help and ideas!!

Answers

  • void setup() { size(525, 680); }
    
    void draw() { background(0); rect(50, 50, 400, 400); }
    
    void keyPressed() { if (key == 'p'){ //pixelates a large portion of the screen loadPixels(); for (int i = 100000; i < pixels.length; i++) { float rand = random(255); color a = color(rand, 110, 200, 100); pixels[i] = a; } updatePixels();
    noLoop(); } if (key == 'e') { //switches two parts of the image PImage p = get(); copy(p, 0, 0, 100, 400, 0, 280, 100, 400); copy(p, 0, 400, 100, 280, 0, 0, 100, 280);
    } }
    
  • I formatted it for you. Maybe try putting else if for e instead of just if. And try putting the background in setup. Of those don't work, then tell me.

  • Oops- sorry about the formatting! And thank you, but it didn't work. I had left else out before because I wanted p to stay when I pressed e

  • You must not use noLoop() in this sketch because as well as stopping the draw() method it also stops the event handling loop. So when you press 'p' key handling is stopped and it can't detect further key presses.

  • edited December 2014

    You must not use noLoop() in this sketch because as well as stopping the draw() method it also stops the event handling loop.

    If noLoop() really stopped the "AWT's Event Dispatch Thread", that'd be the most useless Processing's feature:

    // forum.processing.org/two/discussion/8406/
    // need-to-minimize-papplet-for-keypressed-to-work
    
    void setup() {
      noLoop();
    }
    
    void draw() {
      println(Thread.currentThread(), frameCount);
    }
    
    void keyTyped() {
      print(Thread.currentThread(), "");
      redraw();
    }
    
    void mousePressed() {
      keyTyped();
    }
    
  • Quark, wouldn't loop or redraw remedy that? I was trying to find a way to use the keys cumulatively. And GoToLoop, thanks, but I'm not looking print in the console

  • Okay- I think I got it with an array... hopefully.

    Thank you everyone!

  • If noLoop() really stopped the "AWT's Event Dispatch Thread", that'd be the most useless Processing's feature:

    My mistake - apparently it stops the draw() method and using any drawing methods inside event handlers including things like saveFrame(). :\">

  • edited December 2014

    Just an addendum. When it's normally looping w/ loop(), event callbacks run under the "Animation Thread" too!

    // forum.processing.org/two/discussion/8406/
    // need-to-minimize-papplet-for-keypressed-to-work
    
    void draw() {
      background((color) random(#000000));
    }
    
    void keyTyped() {
      println(Thread.currentThread(), frameCount);
    }
    
    void mousePressed() {
      keyTyped();
    }
    
  • Do Network and Serial Events continue to run if noLoop() is used? Is there a primer somewhere on how events are handled in Processing?

  • edited December 2014

    Only thing noLoop() does is assigning false to looping internal variable.
    Its main effect is that the whole handleDraw() internal function will be skipped.
    Besides regular draw(), following registerMethod()'s callbacks: pre(), draw() & post() will be skipped as well!

    However, most 3rd-party libraries seem not to be affected by it.
    For example, "Video" library and its Movie class works w/ noLoop(). Check out these examples: (*)

    Network & Serial aren't graphical libraries. If Movie works w/ noLoop(), those 2 got no reason not to! O:-)

Sign In or Register to comment.