3
votes

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.

2
Okay, I tried another approach. I created a svsView : NSView subclass (with proper .mm and .h) and tried to override keyUp: (an NSResponder (superclass of NSView) method). Inserting a simple NSLog call and seeing nothing from it, it seems like the method isn't even being called. Am I missing something elementary here? ThanksOld McStopher
IBAction is for action methods, not event methods.Peter Hosey
Thanks, Peter. I nixed the IBAction and had gone back to (void)ing it in a NSView subclass and even overrode acceptsFirstResponder to set the class to first responder, but I'm still not seeing it called. Any suggestions?Old McStopher
I've since opened and built the simple developer example "DotView" which overrides the mouseUp: method of NSView. When adding a keyUp: method, it just NSBeeps when keys are pressed just as it does without the keyUp. So I'm thinking it has something to do with what kind of objects can "listen" for key events or some attribute that needs to be set... Okay, wait, just ran the "BlastApp" example which explicitly has keyUp events. It NSBeeps on non-valid keys, so that's a good place to start. I'm just not seeing what determines if/when the event will be heard.Old McStopher

2 Answers

2
votes

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.

1
votes

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. ;)