Mouse focus with multiple windows

edited March 2018 in Programming Questions

There is here an example how to build a processing program having two windows:https://forum.processing.org/two/discussion/12272/multiple-windows-with-processing-3 My question is how it can be figured out which window is active? And when I am using mouse-events which Window is relevant? I want to handle Mouse events from the two windows.

Tagged:

Answers

  • edited March 2018 Answer ✓

    If done correctly you never have to know which window is active.

    The OS will capture events such as mouse, keyboard etc and forwards them to the active window. Each window will only receive and process it's own events.

  • Focus only in current active window which you activate by clicking within the sketch window. Check the demo below. I added some other things that you don't need btw.

    This demo creates a master window and three slave windows which are randomly placed within your display. Click any of the slave windows.

    Kf

    //REFERENCE: https://github.com/processing/processing/issues/1700
    //REFERENCE: https://forum.processing.org/two/discussion/4712/window-location
    //REFERENCE: https://github.com/processing/processing/wiki/Library-Basics
    
    
    import java.awt.*;
    
    final int UNKNOWN=-1;
    
    int ctr=100;
    int currentWindow=UNKNOWN;
    
    
    void setup() {
      size(300, 100);
      fill(144);
      textAlign(CENTER,CENTER);
    
    
      createSketch(ctr++, "Slave Sketch");
      createSketch(ctr++, "Slave Sketch");
      createSketch(ctr++, "Slave Sketch");
    }
    
    
    
    void draw() {
    
      background(0);
      textSize(24);
      text("MASTER\n"+currentWindow,width/2,height/4);
    
       textSize(10);
      text("Click on any window",width/2,height*0.75);
    }  
    
    
    void createSketch(int handle, String name) {
    
      String[] args ={this.toString()};  //Need to attach current name which is stripped by the new sketch 
      String[] newArgs = {name, str(handle)};
      SecondApplet sa = new SecondApplet();
      PApplet.runSketch(concat(args, newArgs), sa);
    }
    
    void mousePressed(){
      currentWindow=UNKNOWN;
    }
    
    
    public class SecondApplet extends PApplet {
    
      String cname;
      int handle;
    
      public void settings() {
        size(200, 300);
      }
      public void setup() {
        println(args);
        cname=args[0];
        handle=int(args[1]);
        getSurface().setTitle(cname+":"+handle);
    
    
        noFill();
    
        registerMethod("pre", this);
      }
    
      void pre() {
        //Frame needs to exist before you call this method
        getSurface().setLocation((int)random(width, displayWidth-width), (int)random(height, displayHeight-height)); 
        unregisterMethod("pre", this);
      }
    
      public void draw() {
        if (focused==true) {
    
          background(0);
          fill(random(255), random(255), random(255));
          ellipse(width/2, height/2, 50, 50);
          currentWindow=handle;
        } else {
    
          background(255);
          fill(0);
          ellipse(100, 50, 10, 10);
    
          //This is done to save unnecessary calls to draw.
          //Recovers when sketch re-gains focus.
          //Only applies to this demonstration.
          noLoop();
        }
      }
    
      void mousePressed() {
    
        //Recover draw's continuous updates
        loop();
    
        //This next doesn't need to be done here as it is handle by the Window object. 
        //However, not doing this shows that you will have inconsistent nehavior 
        //between the mouse event, the focus action and the draw() method. In other
        //words, it seems that draw is not able to see the that focus was gained on time
        focused=true;
      }
    }
    
Sign In or Register to comment.