-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShortcuts.as
95 lines (72 loc) · 1.92 KB
/
Shortcuts.as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package {
import flash.display.*
import flash.events.*;
import flash.ui.Keyboard;
public class Shortcuts {
private var arrKeys:Array
private var arrKeyCode:Array;
private var __stage:Stage;
public var enabled:Boolean = true;
public function Shortcuts ($stage:Stage):void {
arrKeys = []
arrKeyCode = []
__stage = $stage
__stage.addEventListener(KeyboardEvent.KEY_DOWN, __keyListener);
}
public function add(tecla:String, funcao:Function):void {
arrKeys.push( {tecla:tecla, funcao:funcao } );
}
public function addKey($keyCode:int, funcao:Function):void
{
arrKeyCode.push( {keyCode:$keyCode, funcao:funcao } );
}
public function remove(tecla:String):void {
var code:Number = tecla.charCodeAt(0);
var c:Number
for (var i:int = 0; i < arrKeys.length; i++) {
c = String(arrKeys[i].tecla).charCodeAt(0);
if (c == code) {
arrKeys.splice(i, 1);
}
}
}
public function removeAll():void {
arrKeys = [];
arrKeyCode = [];
__stage.removeEventListener(KeyboardEvent.KEY_DOWN, __keyListener);
__stage = null;
}
private function __keyListener(e:KeyboardEvent):void {
var code:Number = e.charCode;
var keyCode:int = e.keyCode;
var c:Number, fun:Function
var i:int
//se estiver habilitado continua
if (enabled == true)
{
// strings
for (i=0; i < arrKeys.length; i++) {
c = String(arrKeys[i].tecla).charCodeAt(0);
fun = arrKeys[i].funcao;
if (c == code) {
try {
fun();
} catch (e:Error){
}
}
}
// keycodes
for (i = 0; i < arrKeyCode.length; i++) {
c = arrKeyCode[i].keyCode
fun = arrKeyCode[i].funcao;
if (c == keyCode) {
try {
fun();
} catch (e:Error){
}
}
}
}
}
}
}