keyup keydown methods NSViewController
I made an instance of NSViewController and added it as a subview to the main window's content view. I want to be able to capture keyboard events, but I have no idea how to implement it.After some research, I learned I needed to implement acceptsFirstResponder
and开发者_运维技巧 the keyUp:event:
and keyDown:event:
methods in the NSViewController, but after that I still don't have the thing working.
- (void)applicationDidFinishLaunching:(NSNotification*)aNotification {
/* GViewController subview of NSViewController */
GViewController *g = [[GViewController alloc] initWithNibName:@"GViewController" bundle:nil];
[contentView addSubview: g];
}
Those methods have to be present in a subclass of NSView
, not NSViewController
. It also doesn't make any sense to do addSubview:someViewController
; the argument to that method needs to be a view.
override func keyDown(with event: NSEvent) {
super.keyDown(with: event)
Swift.print("Caught a key down: \(event.keyCode)!")
}
override keyDown with event worked for me in swift you can try same method in object-c. check out offical doc https://developer.apple.com/documentation/appkit/nsresponder/1525805-keydown?language=objc
精彩评论