Re: Swift 3 macOS read keyboard
Re: Swift 3 macOS read keyboard
- Subject: Re: Swift 3 macOS read keyboard
- From: Eric Dolecki <email@hidden>
- Date: Mon, 10 Oct 2016 23:00:19 +0000 (UTC)
Can I do number 2? Subclass NSVIEWCONTROLLER and do the key up and down and set accepts first responder true. If it's that simple, cool. I didn't know I might need to use subclassing to capture key events.
Sent from Outlook on my phone.
On Mon, Oct 10, 2016 at 6:36 PM -0400, "Quincey Morris" <email@hidden> wrote:
On Oct 10, 2016, at 15:07 , Graham Cox <email@hidden> wrote:
NSWindow, NSWindowController and NSView, NSViewController all inherit from NSResponder, which provide standard methods for dealing with keyUp and keyDown events.
I agree with everything you said, but a bit of caution is needed with NSViewController subclasses, if the deployment target is earlier than 10.10.
Previously, view controllers were not in the responder chain (unless you wrote code to put them there), and the “responder chain for events” section of the Event Handling Guide doesn’t mention view controllers as any kind of special case, as it does for window controllers.
I believe there are 4 possibilities:
1. Subclass NSView to return true from ‘acceptsFirstResponder’. Otherwise the events will go to the window.
2. Subclass NSViewController to handle keyUp/Down. But it’s also necessary to do #1, and if you want pre-10.10 compatibility, you should override keyUp/Down in the view subclass instead.
3. Subclass NSWindow to handle keyUp/Down.
4. Subclass NSWindowController to handle keyUp/Down.
Of these, #4 is the easiest and least treacherous, but I get the impression Eric wants to centralize his business logic in the view controller and pretty much ignore the window controller (which is a fairly reasonable attitude in these storyboard days). In that case, he can have the window controller call view controller methods, preferably *not* keyUp/Down overrides, but it would make more sense to encapsulate everything in the view/view controller combination, which means #1 and probably #2, depending on deployment.
What I find interesting about this problem is that there’s a really easy solution, except that it’s easy only in the it’s-a-very-*small*-can-of-worms sense. That’s why we love old-school Cocoa.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden