1 package net.systemeD.potlatch2 {
3 import mx.collections.ArrayCollection;
4 import flash.net.SharedObject;
5 import flash.events.EventDispatcher;
6 import flash.events.Event;
9 - this is keyed by a 'name' value, e.g. 'background'/'Bing'. If the user renames the value (e.g. renames the 'Bing'
10 layer to 'Microsoft'), the mapping will be lost
11 - the selectedItem call to getKeyFor generates one of those stupid "warning: unable to bind to property
12 'name' on class 'Object' (class is not an IEventDispatcher)" binding errors
15 public class FunctionKeyManager extends EventDispatcher {
17 private static const GLOBAL_INSTANCE:FunctionKeyManager = new FunctionKeyManager();
18 public static function instance():FunctionKeyManager { return GLOBAL_INSTANCE; }
20 public static var fkeys:Array=['','F1','F2','F3','F4','F5','F6','F7','F8','F9','F10','F11','F12','F13','F14','F15'];
21 [Bindable(event="bogus")]
22 public static var fkeysCollection:ArrayCollection=new ArrayCollection(fkeys);
24 private var keys:Array=[];
25 private var listeners:Object={};
27 public function FunctionKeyManager() {
28 for (var i:uint=1; i<16; i++) {
29 if (SharedObject.getLocal("user_state","/").data['F'+i]) {
30 keys[i]=SharedObject.getLocal("user_state","/").data['F'+i];
35 /* Register a function as the handler for all keypresses with that code (e.g. 'background') */
37 public function registerListener(code:String,f:Function):void {
41 /* Set the code (e.g. 'background') and value (e.g. 'Bing') associated with a key */
43 public function setKey(fkey:uint, code:String, value:String):void {
44 keys[fkey]={ code:code, value:value };
45 var obj:SharedObject=SharedObject.getLocal("user_state","/");
46 obj.setProperty('F'+fkey,{ code:code, value:value });
48 dispatchEvent(new Event("key_changed"));
51 public function setKeyFromFString(key:String, code:String, value:String):void {
53 var oldKey:String=getKeyFor(code,value);
54 keys[Number(oldKey.substr(1))]=null;
55 var obj:SharedObject=SharedObject.getLocal("user_state","/");
56 obj.setProperty(oldKey,null);
59 setKey(Number(key.substr(1)),code,value);
63 /* Find what key, if any, is currently assigned to a given code and value */
65 [Bindable(event="key_changed")]
66 public function getKeyFor(code:String, value:String):String {
67 for (var i:uint=1; i<16; i++) {
68 if (keys[i] && keys[i].code==code && keys[i].value==value) { return 'F'+i; }
73 /* Dispatch function triggered by this key */
75 public function handleKeypress(keycode:uint):Boolean {
76 if (keycode<112 || keycode>126) { return false; }
77 var fkey:uint=keycode-111;
78 if (!keys[fkey]) { return false; }
79 if (keys[fkey].value=='') { return false; }
80 listeners[keys[fkey].code](keys[fkey].value);