RSS LinkedIn Twitter

Simple Flex ShortcutManager (revisited)

August 19th, 2009 Categories: Actionscript, Architecture, Flex 3, Flex 4, Scalability

I’ve put some more thought into my shortcut manager, and have decided on a way to implement the shortcut context, as well as curb the Dictionary explosion that my previous architecture would have had.

The new version uses one Dictionary to store all functions, the keyCode, combo keys, and context is all encoded into the dictionary key. To illustrate here is my new addShortcut() method:

public static function addShortcut(keycode : uint, func : Function, pair : String) : void {
var flags : int = getFlagsFromString(pair);

if (flags > 0)
functionMap[keycode + "-" + flags] = func;

It accepts the same parameters, the only difference is that the pair string is now a dash delimited string, containing the key combinations (i.e. “ctrl“, “ctrl-alt“, “shift-ctrl“, etc). Those are parsed by a method getFlagsFromString() which I will probably get rid of in favor of some named constants on the ShortcutManager class. That will eliminate the need for the additional method, and pair (which will be one of said constants) can be added to the key as-is.

The removeShortcut() is very similar. I haven’t implemented “contexts” yet, but you can visualize how it will be done:

functionMap[keycode + "-" + flags + "-" + context] = func;

Now that there is only one Dictionary, the shortcutHandler() method has become almost trivial. Here it is, in all of its 5-lined glory:

public static function shortcutHandler(event : KeyboardEvent) : void {
var flags : uint = getFlags(event);

if (functionMap[event.keyCode + "-" + flags] != null)
(functionMap[event.keyCode + "-" + flags] as Function).apply();

Like I said… trivial.

Tags: ,
No comments yet.

Leave a Comment