问题
I am creating one cocoa application having wizard like structure.
All dialogs are subclass of NSViewController. Currently I am not able get keyboard events such as keyDown and keyUp..
Please help me to solve this problem....
Thanks in advance....
回答1:
Override keyDown: and keyUp: method.
-(void)keyUp:(NSEvent*)event
-(void)keyDown:(NSEvent*)event
and
- (BOOL)acceptsFirstResponder {
return YES;
}
In subclass of NSViewController
you should refer Cocoa Event-Handling Guide .
回答2:
If you are trying to simply get an event for escape, use this instead:
override var acceptsFirstResponder: Bool {
return true
}
override func cancelOperation(_ sender: Any?) {
// The user pressed escape
}
回答3:
Setting window to "Auto Recalculates View Loop" in Inteface Builder has worked for me.
来源:https://stackoverflow.com/questions/8148714/how-to-handle-keyboard-events-in-subclass-of-nsviewcontroller