Re: Keyboard NSEvents and a NSObjectController?
Re: Keyboard NSEvents and a NSObjectController?
- Subject: Re: Keyboard NSEvents and a NSObjectController?
- From: Quincey Morris <email@hidden>
- Date: Wed, 05 Oct 2011 18:46:20 -0700
On Oct 5, 2011, at 17:28 , Robert Monaghan wrote:
> I have a bunch of Objects that are in an NSArrayController. My UI is bound to an NSObjectController which is then set to work with one of these objects (in the ArrayController). The user essentially selects one of the objects in the ArrayController using a TableView. This is pretty straight forward stuff.
>
> The wrinkle happens when I want to add non-standard keyboard short cuts. For example, I want the user to use a space bar to start and stop a "play" process on my object. I would want to use other keys to modify the object, too.
>
> What is the best way to pass these Keyboard events through an NSObjectController to my target object? Am I just going to have to write a boat load of spaghetti code to these events directly to the objects in question?
The concept of passing keyboard events through a mediating controller to data model objects is so alien to the MVC paradigm that I think you're wasting your time to approach it this way.
If you literally want to capture keyboard events (NSEvent objects), you must use a NSResponder object such as a view. That is, the thing that knows a key has been pressed has to be in your user interface. Additionally, to get a keyboard event, that responder object would have to be the current first responder, and it would then need to generate an action to a specific or non-specific target. Probably the only practical way to do something like this would be to subclass the window (because it's the default first responder when nothing else is, and so you'd be able to capture keyboard events whenever they weren't captured by [say] a text field).
Maybe there are other ways that involve routing keyboard events, but I don't really want to think about it because they're all likely to be pretty lousy.
If I wanted to do something along this line, here's the approach I'd try:
1. Make a Play menu item, and give it a key equivalent of the space character.
This has the advantage of making the behavior both discoverable and accessibility-accessible. That ain't nuthin'.
2. Give the menu item a unique action, and wire it up to First Responder.
3. Implement the action method in a suitable place that's in the responder chain. If this requires a particular kind of window, put it in the window controller. Under other circumstances, you might put it in the app delegate.
4. In the action method, query the array controller to find the selected object.
5. Initiate playback with this object.
6. You'd also want a UI validation method in the object with the action method, that disables or enables the menu item based on what's selected in the array controller.
The above would be a very natural Cocoa way to solve your problem.
P.S. At risk of sounding like a broken record…
There are no objects "in" a NSArrayController. It's a controller, not a collection.
_______________________________________________
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