Intercepting *all* keystrokes in NSView?
Intercepting *all* keystrokes in NSView?
- Subject: Intercepting *all* keystrokes in NSView?
- From: Alex Hall <email@hidden>
- Date: Thu, 29 May 2014 00:57:34 -0400
Hello list,
I have a subclass of NSView that is set to be my window's first responder. It intercepts keystrokes by implementing keyUp: and performKeyEquivalent: but it doesn't do quite what I need.
I have to intercept as many keystrokes as I possibly can, including those bound to menu items or lower-level system calls. For instance, if the user presses cmd-q, I need to be able to intercept, and selectively block, that (no, implementing applicationShouldTerminate: won't do here, as this has to work for all menu items). Moreover, and this may be impossible, I want to intercept VoiceOver keystrokes. I know that VO keystrokes are intercepted well before my app can see them, which is normally a good thing since it means that they almost always work, no matter what an app is doing. In this case, though, it would be very helpful to be able to block them (part of the app is to train people on the use of VO, so I can't have them pressing incorrect keystrokes and not knowing what they did).
Anyway, the immediate concern for me is the menu items, and anything else I can intercept. I can have users disable VoiceOver if I have to, so catching VO keystrokes is secondary. Here's what I have so far:
-(void) keyUp:(NSEvent*) event{
if(! [self handleKeyUpAndKeyEquivalent:event]) [super keyUp:event];
}
-(BOOL) performKeyEquivalent:(NSEvent*) event{
[self handleKeyUpAndKeyEquivalent:event];
return NO;
return [super performKeyEquivalent:event];
}
-(BOOL) handleKeyUpAndKeyEquivalent:(NSEvent*) event{
NSLog(@"Key pressed: %hu. Modifiers: %lu. Character: %@.", [event keyCode], [event modifierFlags], [event charactersIgnoringModifiers]);
return [[self task] checkKeypress:event];
}
First, I know that second return statement in performKeyEquivalent: is never called. I was doing some testing, and I figured it couldn't hurt to leave the second return in there since it will never be called anyway. Second, that call to [[self task] checkKeypress:event] is simply giving a "task" class the event. The class checks to see if the event's key press is what it was looking for, and returns a boolean based on what it finds.
Everything works (well, basically) except those menu item hotkeys. I get a log of them, since my app logs every keystroke it detects, but they aren't stopped. If it matters, my UI is the default window, inside of whose view is my key-capturing NSView subclass instance, inside of which is a basic text field. Please let me know if you need any additional code snippets, and thank you in advance for any suggestions.
--
Have a great day,
Alex Hall
email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden