Domanda

I am writing my first Cocoa application. It's a simple utility app -- a calculator. I've got everything working, the last thing I want to add is I want the app to respond to key press events. So, you could click the "4" button with the mouse, or you could just press the "4" key on the keyboard. I'm reading all the event handling documentation, but I'm not picking up on the recommended approach for getting an NSResponder (or NSView) that handles the event.

I can make a subclass of NSWindow and tell the interface builder to make the main window of that type. Then when I override the keyDown message in my new NSWindow subclass, it gets the events, but I'm not sure how to connect it to my application delegate class (since I basically just want to switch on the key pressed and call the corresponding message in the delegate that the button "select" action targets.

Or, I think I heard something about doing this by changing the super class of the application delegate class from NSObject to NSView, or NSResponder or something. I haven't poked at that idea much, but I'm not sure how to actually get the application delegate object then actually set up as the first responder (I think that's what I'd want to do).

So, I'm hoping someone can give me specific directions on how/where I should implement my keyDown method, but I'd also like to understand what the recommended "best practice" is for doing this. What would Apple say is the right architecture for doing this? I'm as much interested in learning how to make this work as learning the Apple design pattern behind it.

The structure of my program is as follows: I have one nib file (actualy a .xib) where I've designed my window with a textbox for display and a hand-ful of buttons. I've registered outlets and actions from there to the AppDelegate class (the one that the standard project template sets up for you). Following the MVC pattern, I then have a Calculator class that the AppDelegate initializes and all the actual logic of the calculator is in that class. The AppDelegate basically just responds to messages from the buttons, passes them along to methods in the Calculator class, and then asks the Calculator for the current display value and updates that back into the textbox. So, the graphical elements in my nib file are the "view", the AppDelegate is the "controler" and the Calculator class is the "model". Right?

So, who is supposed to be handling keyDown events? Seems like the view should capture them and send them to the controller, but how do I set that up?

I suppose a potentially related question is that i've seen mentions of "window controller" and "view controller" classes. I'm not sure I understand what the point of those classes are. Does my simple app have them? Should it?

È stato utile?

Soluzione

Use NSButton's setKeyEquivalent: method or set the key equivalent for each button in interface builder.

I'm not sure of the "proper" way to do this, but custom key event handling can be put in a custom window contentView like so:

- (void)keyDown:(NSEvent *)theEvent {
    NSLog(@"keyDown:%@", theEvent);

    //Put event handling code here.

    [super keyDown:theEvent];
}

- (BOOL)acceptsFirstResponder {
    return YES;
}

If anyone knows the correct way of doing this, feel free to chime in.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top