Detect if any window in the sketch is the active window

I have a 2+ windows processing Sketch, I want 1 windows to stay on top of all the other windows in the sketch, to do this I am using surface.setAlwaysOnTop(true);, however this puts the window on top of all windows, not just sketch windows so to mitigate this I want to be able to tell if my sketch is the focused window so that I can toggle setAlwaysOnTop depending on if the sketch is on top or not. I tried using focused, but it does not work as expected sometimes being true while no processing sketch is in the foreground, and some times being false after clicking directly on the window. How would I either setAlwaysOnTop just other windows in the sketch OR detect if the sketch is the active window?

we play

with

but only for one window until now?

mouseEnter/Leaves wont work as I need it to only trigger when a new window enters the foreground not on mouse off. As for focused, its not working as expected for me.

can you detail this?
yes it does work,

-0- no focus
-1- main focus
-2- child focus

and that you can not use?

// Processing / Examples / Demos / Tests / Multiple Windows 
// Based on code by GeneKao (https://github.com/GeneKao)
// cut down to 2 windows
// and show focus

ChildApplet child;
boolean mousePressedOnParent = false, mainfocused= false;

void settings() {
  size(320, 240, P3D);
  smooth();
}

void setup() {
  surface.setTitle("Main");
  child = new ChildApplet();
  fill(255);
}

void draw() {
  background(0);
  if (mousePressed) {
    text("Mouse pressed on parent.", 10, 10);
    mousePressedOnParent = true;
  } else   mousePressedOnParent = false;

  if (child.mousePressed)  text("Mouse pressed on child.", 10, 30);
  if ( focused ) { 
    text("Parent focused", 10, 50); 
    mainfocused= true;
  } else   mainfocused= false;
  if ( child.focused )  text("Child focused", 10, 50);
}

void mousePressed() {
}

void mouseDragged() {
}

class ChildApplet extends PApplet {
  public ChildApplet() {
    super();
    PApplet.runSketch(new String[]{this.getClass().getName()}, this);
  }

  public void settings() {
    size(400, 400, P3D);
    smooth();
  }
  public void setup() { 
    surface.setTitle("Child");
    fill(255);
  }

  public void draw() {
    background(0);
    if (mousePressed)         text("Mouse pressed on child.", 10, 30);
    if (mousePressedOnParent) text("Mouse pressed on parent", 10, 20);

    if ( focused )      text("Child focused", 10, 50);
    if ( mainfocused )  text("Parent focused", 10, 50);
  }

  public void mousePressed() {
  }

  public void mouseDragged() {
  }
}


when I try the code

boolean mainFocus=false;
class Second extends PApplet{
  void setup(){
    size(100,100); 
   

  }
  void draw(){
    background(255,0,0); 
    surface.setAlwaysOnTop(mainFocus|focused);
    text(str(focused),50,50);
  }
}
void setup(){
   size(200,200);
   String[] args = {"YourSketchNameHere"};
   Second sa = new Second();
   PApplet.runSketch(args, sa);
   
}
void draw(){
  background(0,255,0); 
  fill(0);
  mainFocus=focused;
  text(str(focused),100,100);
}

I get these results
1st window focused, check


2nd window focused, check

background window focused… not working

note that in the last image the processing window is focused.

did you test my code? did it run?

understand that there parent AND child know about each focus ( and mouseClick )

Your code works, however, when

surface.setAlwaysOnTop(focused||mainfocused);

is added to line 60 it no longer functions

-a- i see

but it should have been solved

-b- but if it would work you would never be able to select (focus)
the main window again
?can that be your idea?

-a- frame was depricated in processing 3.0 replaced with surface, surface.setAlwaysOnTop(true) works fine
-b- I dont think you understand what I am trying to do,
I have a 2 window sketch, 1 window is the main sketch, the other window is a control panel for the 1st sketch. As such the control window must be infront of the other window. However surface.setAlwaysOnTop(true) puts the control window ontop of other unrelated windows (like web browsers). This window should show up behind other active non sketch windows. To achive this I am attempting yo detect if any sketch window has focus, and if one does, set the always ontop state acordingly.

surface.setAlwaysOnTop(true);

:slight_smile: