使用 as3 中的静态方法,我可以从字符串中的字符获取charCode 。
如何从字符串中的字符获取 keyCode?我是否需要通过 javascript 运行此功能(如果我可以在 Javascript 中执行此操作...)
使用 as3 中的静态方法,我可以从字符串中的字符获取charCode 。
如何从字符串中的字符获取 keyCode?我是否需要通过 javascript 运行此功能(如果我可以在 Javascript 中执行此操作...)
复制粘贴为“KeyCodeUtil.as”,放到文件夹下:net/onthewings/utils
/*
KeyCodeUtil.as
Andy Li (andy@onthewings.net)
2009-11-02
*/
package net.onthewings.utils {
import flash.ui.Keyboard;
public class KeyCodeUtil {
/*
The below codes is used for generating KEYCODES. Some formating is needed to be done by hand after getting the outputStr.
var outputStr:String = "";
function onPress(evt:KeyboardEvent):void {
outputStr += "'" + evt.charCode + "':" + evt.keyCode + ", //"+String.fromCharCode(evt.charCode)+"\n";
}
*/
static public const KEYCODES:Object = {
27: 27, //esc
96: 192, //`
49: 49, //1
50: 50, //2
51: 51, //3
52: 52, //4
53: 53, //5
54: 54, //6
55: 55, //7
56: 56, //8
57: 57, //9
48: 48, //0
45: 189, //-
61: 187, //=
8: 8, //backspace
9: 9, //tab
113: 81, //q
119: 87, //w
101: 69, //e
114: 82, //r
116: 84, //t
121: 89, //y
117: 85, //u
105: 73, //i
111: 79, //o
112: 80, //p
91: 219, //[
93: 221, //]
92: 220, //\
97: 65, //a
115: 83, //s
100: 68, //d
102: 70, //f
103: 71, //g
104: 72, //h
106: 74, //j
107: 75, //k
108: 76, //l
59: 186, //;
39: 222, //'
13: 13, //enter
122: 90, //z
120: 88, //x
99: 67, //c
118: 86, //v
98: 66, //b
110: 78, //n
109: 77, //m
44: 188, //,
46: 190, //.
47: 191, ///
32: 32, //space
127: 46, //delete
126: 192, //~
33: 49, //!
64: 50, //@
35: 51, //#
36: 52, //$
37: 53, //%
94: 54, //^
38: 55, //&
42: 56, //*
40: 57, //(
41: 48, //)
95: 189, //_
43: 187, //+
81: 81, //Q
87: 87, //W
69: 69, //E
82: 82, //R
84: 84, //T
89: 89, //Y
85: 85, //U
73: 73, //I
79: 79, //O
80: 80, //P
123: 219, //{
125: 221, //}
124: 220, //|
65: 65, //A
83: 83, //S
68: 68, //D
70: 70, //F
71: 71, //G
72: 72, //H
74: 74, //J
75: 75, //K
76: 76, //L
58: 186, //:
34: 222, //"
90: 90, //Z
88: 88, //X
67: 67, //C
86: 86, //V
66: 66, //B
78: 78, //N
77: 77, //M
60: 188, //<
62: 190, //>
63: 191 //?
}
/**
* @param char Can be String or charCode(Number).
* @param numpad If set to true, the function will return the keyCode for numpad. Ignored if numpad have no such key.
*/
static public function keyCodeOf(char:*, numpad:Boolean = false):Number {
var charCode:Number;
if (char is String)
charCode = (char as String).charCodeAt();
else
charCode = char;
if (numpad){
if (charCode >= 48 && charCode <= 57){ //0-9
return charCode + 48;
} else if (charCode == 43){
return Keyboard.NUMPAD_ADD;
} else if (charCode == 46){
return Keyboard.NUMPAD_DECIMAL;
} else if (charCode == 47){
return Keyboard.NUMPAD_DIVIDE;
} else if (charCode == 13){
return Keyboard.NUMPAD_ENTER;
} else if (charCode == 42){
return Keyboard.NUMPAD_MULTIPLY;
} else if (charCode == 45){
return Keyboard.NUMPAD_SUBTRACT;
}
}
return KEYCODES[charCode] ? KEYCODES[charCode] : -1;
}
}
}
在 Adobe AIR 中使用它:
var str:String = "a"; //only one letter at a time
var keyCode:uint = Keyboard[str.toUpperCase()];
trace(keyCode);
为什么要从字符串中获取键码?为什么不直接从KeyboardEvent获取呢?
以下是文档中示例代码的精简版本,显示了如何处理和检索来自以下位置的键码KeyboardEvent
:
package {
import flash.display.Sprite;
import flash.display.DisplayObject;
import flash.events.*;
public class KeyboardEventExample extends Sprite {
private var child:Sprite = new Sprite();
private var bgColor:uint = 0x00CCFF;
private var size:uint = 80;
public function KeyboardEventExample() {
child.graphics.beginFill(bgColor);
child.graphics.drawRect(0, 0, size, size);
child.graphics.endFill();
addChild(child);
child.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
child.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
}
private function keyDownHandler(event:KeyboardEvent):void {
trace("keyDownHandler: " + event.keyCode);
}
private function keyUpHandler(event:KeyboardEvent):void {
trace("keyUpHandler: " + event.keyCode);
}
}
}
如果您确实有一个需要阅读的字符串并且听键盘不是一种选择(正如 Soviut 建议的那样),那么您将需要确保字符串以某种方式分隔,很可能使用逗号 - 因为字符数是每个 keyCode 都不一样,有些 keyCode 是字符串,有些是 uint。
以下是所有 keyCode 常量的列表:
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/ui/Keyboard.html
我想我会包括一个更简单的方法来做到这一点:
var str:String = "B";
trace(str.charCodeAt(0)); // replace 0 with the index of the char you want the code of