# Detect if any window in the sketch is the active window

**URL:** <https://discourse.processing.org/t/detect-if-any-window-in-the-sketch-is-the-active-window/8989>\
**Category:** Coding Questions\
**Created:** [March 5, 2019, 9:11pm UTC](https://discourse.processing.org/t/detect-if-any-window-in-the-sketch-is-the-active-window/8989 "2019-03-05T21:11:34Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamcdonald](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@jamcdonald](https://discourse.processing.org/u/jamcdonald)\
**Post date:** [March 5, 2019, 9:11pm UTC](https://discourse.processing.org/t/detect-if-any-window-in-the-sketch-is-the-active-window/8989/1 "2019-03-05T21:11:34Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [March 6, 2019, 2:53am UTC](https://discourse.processing.org/t/detect-if-any-window-in-the-sketch-is-the-active-window/8989/2 "2019-03-06T02:53:21Z")

</div>

we play

> [@How to tell if the mouse is over the window?](https://discourse.processing.org/t/how-to-tell-if-the-mouse-is-over-the-window/5285):
>
> I looked through the reference and I don’t see anything for that. Do I just need to use mouseEnter() and mouseLeave() events? confused

with

> **[focused / Reference](https://processing.org/reference/focused.html)**
>
> Confirms if a Processing program is "focused", meaning that it is active and will accept input from mouse or keyboard. This variable is true if it is focused and false if not.

but only for one window until now?

---

<div class="post-metadata">

**Author:** ![jamcdonald](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@jamcdonald](https://discourse.processing.org/u/jamcdonald)\
**Post date:** [March 6, 2019, 7:10am UTC](https://discourse.processing.org/t/detect-if-any-window-in-the-sketch-is-the-active-window/8989/3 "2019-03-06T07:10:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [March 6, 2019, 12:28pm UTC](https://discourse.processing.org/t/detect-if-any-window-in-the-sketch-is-the-active-window/8989/4 "2019-03-06T12:28:47Z")

</div>

> [@jamcdonald](#):
>
> 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?

```auto
// 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() {
  }
}

```

---

<div class="post-metadata">

**Author:** ![jamcdonald](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@jamcdonald](https://discourse.processing.org/u/jamcdonald)\
**Post date:** [March 6, 2019, 6:32pm UTC](https://discourse.processing.org/t/detect-if-any-window-in-the-sketch-is-the-active-window/8989/5 "2019-03-06T18:32:44Z")

</div>

when I try the code

```auto
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

 ![1st%20windows%20focussed](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/493013020d904271ae742c62dd0dea7d84c5d6e2.png)  
2nd window focused, check  
 ![2nd%20windows%20focussed](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/c43680a1d3e87221d6ced22b3981fd50fc847a47.png)  
background window focused… not working  
 ![other%20focused](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/bd732235c0a644e7d22ac8b5d6d4929838a5452e.png)  
note that in the last image the processing window is focused.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [March 7, 2019, 1:57am UTC](https://discourse.processing.org/t/detect-if-any-window-in-the-sketch-is-the-active-window/8989/6 "2019-03-07T01:57:20Z")

</div>

did you test my code? did it run?

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

---

<div class="post-metadata">

**Author:** ![jamcdonald](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@jamcdonald](https://discourse.processing.org/u/jamcdonald)\
**Post date:** [March 7, 2019, 11:53pm UTC](https://discourse.processing.org/t/detect-if-any-window-in-the-sketch-is-the-active-window/8989/7 "2019-03-07T23:53:41Z")

</div>

Your code works, however, when

```auto
surface.setAlwaysOnTop(focused||mainfocused);

```

is added to line 60 it no longer functions

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [March 8, 2019, 1:37am UTC](https://discourse.processing.org/t/detect-if-any-window-in-the-sketch-is-the-active-window/8989/8 "2019-03-08T01:37:05Z")

</div>

> [@jamcdonald](#):
>
> surface.setAlwaysOnTop(focused||mainfocused);

-a- i see

> <https://github.com/processing/processing/issues/3718>
>
> "frame.setAlwaysOnTop(true);" no longer works in the 3.0 version. Has anyone fig…ured out a way to keep the window on top?

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?

---

<div class="post-metadata">

**Author:** ![jamcdonald](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@jamcdonald](https://discourse.processing.org/u/jamcdonald)\
**Post date:** [March 8, 2019, 6:06am UTC](https://discourse.processing.org/t/detect-if-any-window-in-the-sketch-is-the-active-window/8989/9 "2019-03-08T06:06:52Z")

</div>

-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.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [May 18, 2019, 8:30pm UTC](https://discourse.processing.org/t/detect-if-any-window-in-the-sketch-is-the-active-window/8989/10 "2019-05-18T20:30:59Z")

</div>

surface.setAlwaysOnTop(true);

🙂
