How to handle keyboard events in subclass of NSViewController?

↘锁芯ラ 提交于 2019-12-03 18:54:32

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 .

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
}

Setting window to "Auto Recalculates View Loop" in Inteface Builder has worked for me.

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