views:

193

answers:

2

UPDATED: I'm now overriding the NSView keyUp method from a NSView subclass set to first responder like below, but am still not seeing evidence that it is being called.

@implementation svsView

- (BOOL)acceptsFirstResponder {
    return YES;
}

- (void)keyUp:(NSEvent *)event {
  //--do key up stuff--
  NSLog(@"key up'd!");
}

@end

--ORIGINAL POST-- I'm new to Cocoa and Obj-C and am trying to do a (void)keyUp: from within the implementation of my controller class (which itself is of type NSController). I'm not sure if this is the right place to put it, though. I have a series of like buttons each set to a unique key equivalent (IB button attribute) and each calls my (IBAction)keyInput method which then passes the identity of each key onto another object. This runs just fine, but I also want to track when each key is released.

--ORIGINAL [bad] EXAMPLE--

@implementation svsController

//init
//IBActions

- (IBAction)keyInput:(id)sender {
  //--do key down stuff--
}

- (void)keyUp:(NSEvent *)event {
  //--do key up stuff--
}

@end

Upon fail, I also tried the keyUp as an IBAction (instead of void), like the user-defined keyInput is, and hooked it up to the appropriate buttons in Interface Builder, but then keyUp was only called when the keys were down and not when released. (Which I kind of figured would happen.)

Pardon my noobery, but should I be putting this method in another class or doing something differently? Wherever it is, though, I need it be able to access objects owned by the controller class.

Thanks for any insight you may have.

+1  A: 

NSResponder subclasses (such as a custom subclass of NSView) handle key events. They even handle more abstract-level events such as "move left/right", "insert newline", etc. They can then send a message to a controller to respond accordingly.

Joshua Nozzi
Thanks, Joshua. I am in fact subclassing NSView, but for some reason, I cannot get the keyUp method to be called. I wonder if it has to do with the responder chain or some other simple trick I'm missing.
Old McStopher
I was going to suggest making sure your subclass returns YES on -acceptsFirstResponder but I saw your other answer. That'll cause problems too. :-)
Joshua Nozzi
+1  A: 

Wow! I think I've nailed it. I just needed to instantiate my subclass with a NSView/NSWindow reference to the class in IB. Wasn't actually creating an instance of it in the UI. The past several hours down the crapper! Sans that I learned a thing or two along the way. ;)

Old McStopher