Daniel
Daniel

Reputation: 141

get current focused window id using objective c

how can i get current focused window id using objective c, the return value should be an int, help is highly appreciated! I just need to get the following line working

CGImageRef windowImage = CGWindowListCreateImage(CGRectNull, kCGWindowListOptionIncludingWindow, winNum, kCGWindowImageBoundsIgnoreFraming);

but no matter which window that i focus at, winNum is always 0.

Upvotes: 0

Views: 3930

Answers (1)

Kenny Winker
Kenny Winker

Reputation: 12107

NSWindow *keyTarget = [[NSApplication sharedApplication] keyWindow];
NSInteger winNum = [keyTarget windowNumber];

or in one line:

[[[NSApplication sharedApplication] keyWindow] windowNumber];

Upvotes: 7

Related Questions