I have started listening to global keyDown
events. Is there a way to get information from which application that event came?
A handler receives NSNotification
instance and NSEvent
is part of it. Can I somehow extract that information from those objects?
Listening snippet:
[NSEvent addGlobalMonitorForEventsMatchingMask:NSKeyDownMask handler:^(NSEvent *event){
NSLog(@"global keyDown %@", event);
[[NSNotificationCenter defaultCenter] postNotificationName:kKeyPressed
object:event];
}];
Observer:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyEventHandler:)
name:kKeyPressed
object:nil];
UPDATE
Global key downs are not send from any particular application. What I actually needed is to check for currently active application at event handler:
[[NSWorkspace sharedWorkspace] activeApplication]
This returns NSDictionary
with information I needed.