I was looking to intercept Command key combinations and thought that IMKit
would be a good choice. By extending IMKInputController
I can intercept most keys but it seems to ignore modified ones.
I've tried overriding
-(BOOL)inputText:(NSString*)string client:(id)sender;
and (alternatively)
-(BOOL)inputText:(NSString*)string
key:(NSInteger)keyCode
modifiers:(NSUInteger)flags
client:(id)sender;
but no luck; the methods just plain aren't called when the modifiers are applied. To be more specific the command
and alt
key don't get caught by the methods above. Simple modifiers like shift
and ctrl
work (and the modifier flags
variable is set in the second method). Fire up Apple's sample application NumberInput to see for yourself.
Any suggestions? Am I on totally the wrong track?
Short answer:
Use
handleEvent:client:
and listen to theNSFlagsChanged
event.
IMKInputController
implements the IMKServerInput Protocol, which provides three alternatives to handle an event.
inputText:client:
and didCommandBySelector:client:
inputText:key:modifiers:client:
handleEvent:client:
Seems like you've only tried the top two. You can achieve the goal with the third option.
Try the following:
override recognizedEvents:
(from IMKStateSetting Protocol)
- (NSUInteger)recognizedEvents:(id)sender
{
return NSKeyDownMask | NSFlagsChangedMask;
}
and use handleEvent:client:
-(BOOL)handleEvent:(NSEvent*)event client:(id)sender
{
NSLog(@"handling event: %@", event);
return false;
}
You can see the printout on every keydown and keyup of the modifiers in Console, including command
and alt
.