Re: Another best practice question: storing hotkey assignments
Re: Another best practice question: storing hotkey assignments
- Subject: Re: Another best practice question: storing hotkey assignments
- From: Alex Hall <email@hidden>
- Date: Sat, 04 Jan 2014 16:58:28 -0500
On Jan 4, 2014, at 2:14 PM, Steve Mills <email@hidden> wrote:
> On Jan 4, 2014, at 10:13:46, Alex Hall <email@hidden> wrote:
>
>> Yes, I can do it, but it seems like there must be a more efficient way than or-ing every combination of those five masks with the modifier flags each keypress contains. I can't be the first to have this question, but google is oddly quiet on the topic and I just keep ending up at the Event Handling Guide or other sites that aren't what I'm looking for. As always, any suggestions are greatly appreciated.
>
> Since there are only 5 modifiers (6 if you count num lock, 7 if you count fn), all modifiers could fit into a single byte as individual bits. And since there are less than 128 keys on even a full size keyboard, the key value could also fit into a single byte. Squish them together into a UInt16 and there's something you can easily use as the key in a dictionary (or std::map if you prefer). Or use a full UInt32 so you'll have room to expand if they add more modifiers or keys in the future. The key events you get from the OS already have the mods packed into 16 bits, so you really wouldn't even need to redefine your own constants for them, and the key code is also already a short. There ya go, 2 shorts make UInt32.
> Got you. So, here's some sample code of how I'm thinking this could work. Can anyone see flaws in it? I mostly ask because, during testing of keystroke capturing, I noticed modifierFlags was often 256 even when I had not pressed any modifiers at all. I know my function signatures might be wrong, and there is no error-checking, but this gives you the idea.
//store a keystroke
NSUInteger key=[event keyCode]+[event modifierFlags];
SEL funcToCall=@selector(myFunction:);
NSDictionary* hotkeys=@{key:funcToCall};
-(void) keyDown:(NSEvent) event{
SEL func=[hotkeys ObjectForKey:[event keyCode]+[event modifierFlags]];
[self performSelector:func];
}
> --
> Steve Mills
> office: 952-818-3871
> home: 952-401-6255
>
>
>
> _______________________________________________
>
> 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
Have a great day,
Alex (msg sent from Mac Mini)
email@hidden
_______________________________________________
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