keyPressed() inside a class [Eclipse]

edited November 2013 in Programming Questions

is there any way to do this? i found this thread ( http://forum.processing.org/one/topic/using-keypressed-in-custom-keyboard-handler-class-eclipse.html) from the old forum but i did not quite understand PshiLHo`s answer. can someone make it a bit clearer? example

public class GUI{



//call keypPressed some how in here

}

and the class with setup() and draw()

public class GUIMain extends PApplet{

void setup(){
}

void draw(){

}

public static void main(String[] args) {
        PApplet.main(new String[] { GUIMain.class.getName() });

    }

}

i need to make a small gui and i think it will better to code interactivity inside this class

thanks

Answers

  • edited November 2013

    Based on my interpretation of post you linked to

    public class GUI{
    
      private PApplet app;
    
      public GUI(PApplet app) {
        super();    // this is optional for this class
        this.app = app;
        // Any other setup code required for this class
      }
    
      public void keyPressed(){
        // use app.key in this method
      }
    
    }
    
    
    public class GUIMain extends PApplet{
    
      private GUI gui;
    
      public void setup(){
    
        gui = new GUI(this);
    
      }  
    
      public void draw(){
    
      }
      public void keyPressed(){
        gui.keyPressed();
      }
    
      public static void main(String[] args) {
        PApplet.main(new String[] { GUIMain.class.getName() });
    
      }
    
    }
    
  • edited November 2013 Answer ✓

    This is another way of doing it which gives your class greater control. The code registers your gui object to receive mouse and key events.

    public class GUI{
    
      private PApplet app;
    
      public GUI(PApplet app) {
        super();    // this is optional for this class
        this.app = app;
        // Any other setup code required for this class
      }
    
      public void mouseEvent(MouseEvent e){
        int mx = app.mouseX;
        int my = app.mouseY;
    
      }
    
      public void keyEvent(KeyEvent e) { 
        // some useful variables that should be self explanitory
        char keyChar = e.getKey();
        int keyCode = e.getKeyCode();
        int keyID = e.getAction();
        boolean shiftDown = e.isShiftDown();
        boolean ctrlDown = e.isControlDown();
    
      }
    
    }
    
    
    public class GUIMain extends PApplet{
    
      private GUI gui;
    
      public void setup(){
    
        gui = new GUI(this);
        registerMethod("mouseEvent", gui);
        registerMethod("keyEvent", gui);
      } 
    
      public void draw(){
    
      }
    
      public static void main(String[] args) {
        PApplet.main(new String[] { GUIMain.class.getName() });
    
      }
    
    }
    
  • edited November 2013

    thanks Quark! the second way is the one i was looking for.

    (email notifications do not work in this forum or is it just me? i have them enabled)

Sign In or Register to comment.