How to detect if the window is currently active?

Hi, I would like to know if there’s any possible way to detect if the OF window is currently active or not.
I think some other windowing APIs have this option named such as ‘gotFocus’ and ‘lostFocus’.
I tried to look for such option in OF but I couldn’t. Maybe it’s not possible yet?

Alright, here’s the way I found.

First, include glfw in your file where you will be creating a window.

**#define GLFW_INCLUDE_NONE**
**#include "GLFW/glfw3.h"**

Then, create a window using ofGLFWWindowSettings.

ofGLFWWindowSettings settings;
ofCreateWindow(settings);

then, create a function that will listen to focus of the window.

void window_focus_callback(GLFWwindow* window, int focused)
{
    if (focused)
    {
        // The window gained input focus
    }
    else
    {
        // The window lost input focus
    }
}

then, register the callback function by doing this.

glfwSetWindowFocusCallback(((ofAppGLFWWindow*)ofGetAppPtr())->getGLFWWindow(), window_focus_callback);

And you will be able to detect the focus of the window :slight_smile:

This way, you can also use other GLFW features you need to use.
I found the info from http://www.glfw.org/docs/latest/window_guide.html.

Do you have any hints on getting this to work on of10 ?
I put all the functions in ofMain but the ((ofAppGLFWWindow*)ofGetAppPtr())->getGLFWWindow() call is always causing a segfault:

Ideally I would also like to callback a function in ofApp to display a prompt for the user to focus on the window… any tips on how to do that?

Here’s my main function:

#include "ofMain.h"
#include "ofApp.h"
#include "ofxJsonSettings.h"
#define GLFW_INCLUDE_NONE
#include "GLFW/glfw3.h"
//========================================================================

void window_focus_callback(GLFWwindow* window, int focused)
{
    if (focused)
    {
        ofLogError("test");
        // The window gained input focus
    }
    else
    {
        ofLogError("test");
        // The window lost input focus
    }
}

int main( ){
    ofGLFWWindowSettings settings;
    Settings::get().load("settings.json");

    settings.setSize(1920,1080);
    settings.monitor =Settings::getInt("monitor_index");
    settings.windowMode = Settings::getBool("windowed") ? OF_WINDOW :OF_FULLSCREEN;

    ofCreateWindow(settings);
    glfwSetWindowFocusCallback(((ofAppGLFWWindow*)ofGetAppPtr())->getGLFWWindow(), window_focus_callback );
    ofRunApp(new ofApp());
}

I figured it out . This us what I put in my main function after ofCreateWindow():

auto app = new ofApp();
glfwSetWindowFocusCallback(((ofAppGLFWWindow*) ofGetWindowPtr())->getGLFWWindow(), app->window_focus_callback );
ofRunApp(app); 

hi im getting this issue when trying to implementing this

: ‘class ofApp’ has no member named ‘window_focus_callback’
32 | glfwSetWindowFocusCallback(((ofAppGLFWWindow*) ofGetWindowPtr())->getGLFWWindow(), app->window_focus_callback );
| ^~~~~~~~~~~~~~~~~~~~~

any idea how to fix this?

If you just want to test if the window current has the keyboard focus, on Windows, you can use:

if (GetFocus() == ofGetWin32Window())

or if you want to know if it’s the foreground window (very often the same), you can use

if (GetForegroundWindow() == ofGetWin32Window())

hi, im trying this on linux is there. something similar for glfww

Hmm. I’m not in a position to test on Linux at the moment, but some possibilities include these functions which I think will tell you which xwindow has the focus:

Xlib has XGetInputFocus()

libxdo has xdo_get_focused_window_sane()