问题
I have an application that processes keystrokes via keyDown.
This works well so far, with the exception of two problems (I'll post/ask the other later).
The problem here is the Shift+Tab combo. I would like to process it myself, but as explained in the Mac OS keyboard event doc, it is used to move the focus forward.
I tried capturing it under
-(BOOL)performKeyEquivalent:(NSEvent *)nsevent
{
}
where I do get an event for it, returning TRUE or FALSE but it still does not appear in keyDown:
I also know about selectNextKeyView: but ideally I am looking for a way to get the system to pass the combination to keyDown: for normal handling (incidentally keyUp: is called correctly for it).
-(void)selectNextKeyView:(id)sender
{
// shift+tabbed
}
回答1:
Answering my own question:
I solved it by intercepting selectNextKeyView and selectPreviousKeyView and forwarding the [NSApp currentEvent] to my keyboard handler (the one that is called on keyDown: for all other keys)
-(void)selectNextKeyView:(id)sender
{
// this is part of the keyboard handling. Cocoa swallows ctrl+Tab and
// calls sselectNextKeyView, so we create a matchin key event for the occasion
NSEvent *nsevent= [NSApp currentEvent];
[self handleKeyEvent:nsevent isRaw:FALSE isUp:FALSE];
}
-(void)selectPreviousKeyView:(id)sender
{
// this is part of the keyboard handling. Cocoa swallows ctrl+shift+Tab and
// calls selectPreviousKeyView, so we create a matchin key event for the occasion
NSEvent *nsevent= [NSApp currentEvent];
[self handleKeyEvent:nsevent isRaw:FALSE isUp:FALSE];
}
来源:https://stackoverflow.com/questions/40088407/objective-c-on-macos-get-keydown-for-shifttab-in-nsview