Difference between revisions of "Tutorials/C Programming Tutorial/How to find out which window has the focus"

From ThorstensHome
Jump to: navigation, search
(See also)
Line 34: Line 34:
= See also =
= See also =
* [[Tutorials/Hacking_KDE#Find_out_focus_window]]
* [[Hacking_KDE#Find_out_the_active_window_on_your_desktop]]

Latest revision as of 20:57, 15 January 2010

You can use the following source code to find out which window has the focus on your Linux desktop:


#include <stdio.h>
#include <X11/Xlib.h>

/* Compile this using
g++ -lXtst main.cpp

int main()
  Display* display= XOpenDisplay(NULL);
  char* name="blahblah";
  Window window=0;
  int i=0;
  XGetInputFocus(display, &window, &i);
  int  status = XFetchName (display, window , &name);

  XCloseDisplay( display );

Compile, link and run it

g++ -lXtst main.cpp

Now the strange thing is, this worked. But now, kwrite and firefox's window titles are not correctly recognized. However, the programs xprop and xwininfo work correctly. So I analyzed the code and found they are using XGetWMName instead of XFetchName.

See also