How to handle keyboard events in subclass of NSViewController?

大憨熊 提交于 2019-12-05 02:24:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!