Re: "Easter Egg" style key combination changes (Probably not interesting to any one but newbies)
Re: "Easter Egg" style key combination changes (Probably not interesting to any one but newbies)
- Subject: Re: "Easter Egg" style key combination changes (Probably not interesting to any one but newbies)
- From: Development <email@hidden>
- Date: Sun, 20 Jan 2008 14:39:23 -0700
My experiment and it's results:
Lets pretend you want a hidden menu only available when certain keys
are held down. Further more you do not want some one to just open the
nib and see that the menu actually exists some where... I have a
similar issue and after mailing the list and playing with it found one
way to accomplish this. Below is the code which will build the hidden
menu programatically and the nib selections I used.
NSMenuItem * altAbout = [[NSMenuItem alloc]initWithTitle:@"Alternate
About Menu" action:@selector(openAbout:) keyEquivalent:@"!"]; //The
@"!" key equiv seems to negate any kind of equivalent but will allow a
key press change. I suspect every one but me knew this.
[altAbout setTag:242]; //Any number other than the original menu
item's number will do
[altAbout setKeyEquivalentModifierMask:(NSShiftKeyMask |
NSAlternateKeyMask | NSCommandKeyMask | NSControlKeyMask)]; //just
include the modifier(s) you want
[altAbout setTarget:self];
[altAbout setAlternate:YES];
[[appMenu submenu]insertItem:altAbout atIndex:1]; // appMenu is
defined in the header and linked in the nib. You want to insert this
new item just below whatever index the item it is an alt for resides
for of course, obvious reasons.
Now In the NIB I did the following:
First, I connected the 'appMenu' NSMenuItem I declared in the Header
to the actual application menu, this step is of course obvious. Next,
I selected the About <Application Name> Menu Item and checked the
Alternate option(A step I am sure would either cause problems for an
item several indexes down the list, or in this case is completely
meaningless since the option only refers to being an alt to it's
predecessor.) Next For the Key equiv I held shift, apple, 1 (Which of
course made the Equiv command-!) and as near as I can tell it NULLs
the equiv but leaves a modifier in place. You will also want to make
sure that both the alternate and original menu items point to the same
selector. In the selector you can use the [sender tag] to figure out
which sent the message. I say this because when I had them pointed to
different selectors it crashed the application when I selected the
alt, then released the keys and tried to open the application menu. I
am sure this would be the obvious result to every one but me though.
Then build the app, launch, click the App menu and see the original,
then hold the modifiers you've designated and whala it changes.
Now, as I look at the code I am fairly sure it is easier to do this
than what I've outlined but meh, it works so it'll get you started. In
other words I suspect that setting the key equiv to null or @"" or
something, then setting the modifier mask would have the same effect
but I didn't try it and since this works I probably won't.
Forgive the N00b explanation, but I have always found that more
information or explanation than I needed is better than not enough.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden