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).
Second version of the same methods not using virtual key codes:
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)