开发者

keyup keydown methods NSViewController

开发者 https://www.devze.com 2023-03-09 03:57 出处:网络
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 res

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号