pkk
pkk

Reputation: 1

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];
}

Upvotes: 0

Views: 1372

Answers (2)

Shubham Sharma
Shubham Sharma

Reputation: 499

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

Upvotes: 0

jscs
jscs

Reputation: 64002

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.

Upvotes: 2

Related Questions