RSS LinkedIn Twitter

Contextual Shortcut Manager

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

I’m about ready to sleep on my ShortcutManager, I figured I might as well finish off the series. I added context to it, while still maintaining backward compatibility with the non-context enabled version. I don’t think I posted it, so it probably won’t matter so much to my readers. For those who haven’t read the previous posts on my ShortcutManager, the “context” I’m referring to is the context in which a keyboard shortcut might be used; such that a given shortcut might have several functions associated with it depending on its context.

In short I needed four things to implement the context aware shortcut manager.

1. The IKeyboardContext interface

This is a simple interface that a model or class can implement that allows a keyboardContext to be set and retrieved. A keyboardContext is simply a string that identifies the current context. These contexts will be associated with a shortcut in the manager. Here is the entire interface:

[as3]
package com.googolflex.gflib.interfaces {

public interface IKeyboardContext {

[Bindable(event="propertyChange")]
function get keyboardContext() : String;
function set keyboardContext(v : String) : void;
}
}
[/as3]

2. A reference to an IKeyboardContext needed to be added to the ShortcutManager

As the keyboardContext changes in the model, the ShortcutManager needs to be able to access it, so it can retrieve the correct function for a given keycode-flags-context tuple. The IKeyboardContext was left null, which helps with the backward compatibility (the implementer will be forced to set it if she wants to use it). Here is the line I added, which is pretty trivial:

[as3]public static var model : IKeyboardContext = null;[/as3]

3. Add the context to the addShortcut method

I gave it a default value, which incidentally was “default”, so that every function added to the Dictionary would have some kind of context. Here is the new addShortcut() method:

[as3]
public static function addShortcut(keycode : uint, func : Function, flags : uint, context : String = "default") : void {
if (flags > 0 && flags < 8)
functionMap[keycode + "-" + flags + "-" + context] = func;
}
[/as3]

4. Finally, the shortcutHandler() needed to incorporate the context

I first check if the IKeyboardContext is null, if it is I know to use “default” as the context. If no context was ever specified, and no IKeyboardContext was ever set, then it still works. Here is the modified code:

[as3]
public static function shortcutHandler(event : KeyboardEvent) : void {
var flags : uint = getFlags(event);
var context : String = (model!=null) ? model.keyboardContext : "default";

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

And was pretty much it. I will post the modified code, along with a demonstration application at the end of the post. I do want to make a couple points to be aware of when actually incorporating this into a project.

First, your model/class will need to implement IKeyboardContext. Second, you need to remember to set ShortcutManager.model = myIKeyboardContext somewhere. Third, you’re on your own when it comes to maintaining your context. Maybe the “SimpleContextManager” will be next, who knows? And fourth, you’ll have to specify a context when you call addShortcut(). Remember if you download the demo, it will need to have some kind of reference to the ShortcutManager code.

Tags: ,

2 Responses to “Contextual Shortcut Manager”

  1. kalmah
    February 5th, 2012 at 04:36
    1

    hello, I searched everywhere for implementing contextual shortcuts and you were the only one I came across. Have you created a working demo of this same tutorial? ShortcutManager.zip and KeyboardContextDemo.zip works but since the “context” part still hasn’t been implemented, it’s incomplete. I would very much like to get this working…

  2. kalmah
    February 5th, 2012 at 04:40
    2

    hello, I searched everywhere for implementing contextual shortcuts in Flex and you wre the only one I came across who had really created a good manager. Have you created a working demo of this same tutorial? ShortcutManager.zip and KeyboardContextDemo.zip works but since the “context” part still hasn’t been implemented, it’s incomplete. I would very much like to get this working… Can you please help me out.
    Thanks!

Leave a Comment

*