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: Steve Mills <email@hidden>
- Date: Sat, 04 Jan 2014 13:14:24 -0600
On Jan 4, 2014, at 10:13:46, Alex Hall <email@hidden> wrote:
> As I build the map and the rest of the game engine, I'll need to detect and match up keystrokes. I want the user to be able to define these if they want to, plus I know there has to be a better way to store them than hard-coded in a bunch of if or switch statements. So, my first thought was a dictionary, with the key assignment as the dictionary key and a selector as the value. However, modifiers are tripping me up. I can get the modifiers, and compare each modifier mask to see if that modifier was pressed, but what about two or more modifiers? I'd have to detect every combination of control, option, command, shift, and numpad. Also, storing the hotkeys as strings would mean constructing a string based on which modifiers were pressed, similar to the hotkey definitions in OS10's files for handling keys. 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.
--
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