Using keyboard binding as shortcut for NSButton

Hi all,

I have a small problem which I am not sure, I fully understand. In my application, I have an NSButton
which has to be bound to a certain keyboard shortut(in my case the spacebar). I implemented the whole thing and tested it on my Mac(v10.5.8), everything worked fine as I sended the application to the client he said there was a problem with the button's short cut under 10.5.8 OS X and under 10.6. it works... Beneath is the code that I have wrote and I personally do not see any problem with it. Please post any suggestion as to why the piece of ode would work on one and refuse to function on another machine (both versions 10.5.8).


//The mouseUp event handler in MyController.m which just tells the NSTextFiled to stop editing. //(This is ok because the mouseUp event is only called when the user clicks somewhere outside of //the text box, I have only one so there aren't many things that can go wrong I hope.)
- (void)mouseUp:(NSEvent *)event
{
[[self window] endEditingFor:txtBoxFileName];
}

//The keyDown and keyUp event handlers in MyController.m. The keyDown event filters the keys //and reacts only if the key is the Space Bar (this is also safe, I believe, beause the keyDown or //keyUp events are never called if the focus is on the NSTextField). After that I just send the proper //message as it was send by the button itself.
- (void)keyDown:(NSEvent *)event
{
/* Space bar hex virtual key code is 0x32 */
unsigned int virtualKeyCode = [event keyCode];
if(virtualKeyCode == 49) //is Space Bar?
{
//Send proper message...
}
}

- (void)keyUp:(NSEvent *)event
{
/* Space bar hex virtual key code is 0x32 */
unsigned int virtualKeyCode = [event keyCode];
if(virtualKeyCode == 49) //is Space Bar?
{
//Do stuff...
}
}


Second version of the same methods not using virtual key codes:



//The keyDown and keyUp event handlers in MyController.m. The keyDown event filters the keys //and reacts only if the key is the Space Bar (this is also safe, I believe, beause the keyDown or //keyUp events are never called if the focus is on the NSTextField). After that I just send the proper //message as it was send by the button itself.
- (void)keyDown:(NSEvent *)event
{
NSString* testStr = [[NSString alloc] init];
testStr = [event characters];
if([testStr isEqual:@" "])
{
//Send proper message...
}
}

- (void)keyUp:(NSEvent *)event
{
NSString* testStr = [[NSString alloc] init];
testStr = [event characters];
if([testStr isEqual:@" "])
{
//Do stuff...
}
}

Mac OS X (10.5.8)

Posted on Nov 26, 2009 3:06 AM

Reply
2 replies

Dec 5, 2009 5:49 PM in response to artOf...

Curious, Art, if you were ever able to resolve this as I'm doing something similar.

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

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.

Dec 7, 2009 11:48 PM in response to artOf...

Hi Old McStopher,

I am not so experienced with Mac programing myself, I just finished my first solo big application, so you might want
to give my answer the benefit of a doubt. That said, to answer your first question yes and no - I mean, I have tested
the two above mentioned methods on another Mac machine(v 10.5.8) and it runs flawless there too, so I just figured
the problem should be with my client's Mac. To answer your main question (as best as I can): When I implemented
my solution of the problem I wondered the same as you if I can't use an IBAction and I tried it but it seems not to work
togather with the keyUp method. That is why I did both keyDown (basically what my IBAction should have done) and
keyUp - ending/starting the action/another action. Of course as I imagine it, your keyDown method will be far more
tedious to implement than mine, but I don't see a problem to writing it all in your controller class. May be what you are
missing is that in my application I implemented a new NSButton class where I use mouseUp/mouseDown to invoke the
button's functionality and in MyController.m class I use the keyUp/keyDown to do it, so in fact I don't use the IBAction
at all. Hope that helps and is not too late!
Have fun!

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Using keyboard binding as shortcut for NSButton

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.