setKeyEquivalent and function keys
setKeyEquivalent and function keys
- Subject: setKeyEquivalent and function keys
- From: "Andrew M. Salamon" <email@hidden>
- Date: Tue, 29 Oct 2002 13:40:45 -0800
I already submitted this to RadarWeb, but I thought I'd ask here in
case anyone knows of a workaround. Or perhaps I'm just doing something
wrong.
I'm trying to allow the user to change menu key equivalents. Everything
works just fine so long as you stick to simple letters as the key, but
I'm having problems getting function keys to work in any but the
simplest case. Below is sample code that demonstrates the problem.
Create a new project and add two files with a simple class that I
called AppDelegate. Open the main menu nib, instantiate an AppDelegate
and hook up the outlet and action to a menu item that doesn't already
have a keyboard shortcut. Then run the app. Choose the menu using the
mouse. The first time it will correctly set the keyboard equivalent to
'F12'. Typing F12 at this point also works, and causes the code to try
to change the key equivalent to Command-k. At this point the menu item
will show 'CommandF12' (with Command actually being a cloverleaf), this
is not correct, but typing Command-k will still activate the menu and
cause it to change back to 'F12'. But, the F12 key will no longer
invoke the menu, I just get a beep. Choosing the menu with the mouse
continues to work, as does Command-k, every other time you choose the
menu.
Any suggestions?
Andrew
email@hidden
#import <Cocoa/Cocoa.h>
@interface AppDelegate : NSObject {
IBOutlet NSMenuItem *testMenu;
BOOL functionKey;
}
- (IBAction)toggleMenuKey:(id)sender;
@end
------------
#import "AppDelegate.h"
@implementation AppDelegate
- (IBAction)toggleMenuKey:(id)sender
{
if( testMenu )
{
if( functionKey )
{
[testMenu setKeyEquivalentModifierMask:NSCommandKeyMask];
[testMenu setKeyEquivalent:@"k"];
NSLog( @"Set to Command-k" );
}
else
{
unichar ch[4];
ch[0] = NSF12FunctionKey;
[testMenu setKeyEquivalentModifierMask:NSFunctionKeyMask];
[testMenu setKeyEquivalent:[NSString
stringWithCharacters:ch length:1]];
NSLog( @"Set to F12" );
}
functionKey = !functionKey;
}
else
{
NSLog( @"testMenu was not hooked up." );
}
}
@end
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.