Get title of window that has key focus

Hi, sorry if this is the wrong forum... I'm looking to get the title of the window that has keyboard input focus, but this window will NOT be one of my windows. I've been able to determine a way to get the titles of the windows that are not minimized with the code below found on stack overflow. But this code uses deprecated and undocumented calls. I theorize that calling CGSCopyWindowProperty() with some sort of property that indicates if the window has keyboard focus is possible but I can't find documentation on the other Window Properties. I'm open to a completely different way of doing any/all of this. My code and the code below is in objective-c.


Thanks, Mike


CFStringRef titleValue;

CGSConnection connection = CGSDefaultConnectionForThread();

NSInteger windowCount, *windows;


NSCountWindows(&windowCount);

windows = (NSInteger*) malloc(windowCount * sizeof(NSInteger));

if (windows)

{

NSWindowList(windowCount, windows);

for (int i = 0; i < windowCount; ++i)

{

CGSCopyWindowProperty(connection, windows[i], CFSTR("kCGSWindowTitle"), &titleValue);


if(!titleValue) //Not every window has a title

continue;


//Do something with titleValue here

NSLog(@"%@", titleValue);

}

free(windows);

}

Replies

Anyone have other ideas on obtaining the Window title of a window that has key focus but is NOT owned by my application??

I don't think you can know this from your application ; because of privacy.

Tossing a bone...https://stackoverflow.com/questions/5292204/macosx-get-foremost-window-title