Read keyPressed in Thread

owhowh
edited February 2015 in How To...

Is it possible to override the keyPressed() in a Thread class? I would like to have the Thread class to have a different actions on keyPressed() from the main sketch. Here's what I mean:

// --- Main sketch ---

threadClass thread1;

void setup() {
  thread1 = new threadClass;
  thread1.start();
}

void draw() {}

void keyPressed() {
  // actions by main sketch
}

// --- threadClass ---

class threadClass extends Thread {

  threadClass() {}

  void start() {}

  void run() {
    // loop
  }

  void keyPressed() {
    // specific to threadClass
    // this has no effect (no error as well), how to get it work?
  }

} 

Basically, I want the threadClass has its on keyPressed routine. I can use the main sketch keyPressed() and pass parameters, but it will be better if the threadClass can detect and read keys by itself. Is this possible? How can I achieve this?

I have tried passing PApplet into the threadClass but PApplet.keyPressed() is not valid.

Thanks in advance for any help rendered.

Answers

  • edited February 2014

    I believe we gotta use hidden function -> registerMethod("", this); inside your class. Like this:

    threadClass() {
      registerMethod("keyPressed", this);
    }
    

    A forum thread about it:
    forum.processing.org/two/discussion/1096/keypressed-inside-a-class-eclipse

    Be warned! I haven't tested it yet. You gotta check that out by yourself! [..]

  • edited February 2015

    i am facing the same problem,can anyone help. I want that all the keypressed functions should run in a separate thread.

  • My apology for getting back really late. I have only received an email notification on your replies today! Yes, more than a year later.

    I feel obliged to get back to say thank you. I did not proceed with this matter.

Sign In or Register to comment.