新增虚拟输入类 VirtualInput
This commit is contained in:
@@ -55,6 +55,8 @@ module es {
|
||||
return this._gameTouchs[0].position;
|
||||
}
|
||||
|
||||
public static _virtualInputs: VirtualInput[] = [];
|
||||
|
||||
/** 获取最大触摸数 */
|
||||
public static get maxSupportedTouch() {
|
||||
return Core._instance.stage.maxTouches;
|
||||
@@ -91,11 +93,45 @@ module es {
|
||||
this.initTouchCache();
|
||||
}
|
||||
|
||||
public static update(){
|
||||
KeyboardUtils.update();
|
||||
for (let i = 0; i < this._virtualInputs.length; i ++)
|
||||
this._virtualInputs[i].update();
|
||||
}
|
||||
|
||||
public static scaledPosition(position: Vector2) {
|
||||
let scaledPos = new Vector2(position.x - this._resolutionOffset.x, position.y - this._resolutionOffset.y);
|
||||
return Vector2.multiply(scaledPos, this.resolutionScale);
|
||||
}
|
||||
|
||||
/**
|
||||
* 只有在当前帧按下并且在上一帧没有按下时才算press
|
||||
* @param key
|
||||
*/
|
||||
public static isKeyPressed(key: Keys): boolean{
|
||||
return KeyboardUtils.currentKeys.contains(key) && !KeyboardUtils.previousKeys.contains(key);
|
||||
}
|
||||
|
||||
public static isKeyPressedBoth(keyA: Keys, keyB: Keys){
|
||||
return this.isKeyPressed(keyA) || this.isKeyPressed(keyB);
|
||||
}
|
||||
|
||||
public static isKeyDown(key: Keys): boolean {
|
||||
return KeyboardUtils.currentKeys.contains(key);
|
||||
}
|
||||
|
||||
public static isKeyDownBoth(keyA: Keys, keyB: Keys){
|
||||
return this.isKeyDown(keyA) || this.isKeyDown(keyB);
|
||||
}
|
||||
|
||||
public static isKeyReleased(key: Keys){
|
||||
return !KeyboardUtils.currentKeys.contains(key) && KeyboardUtils.previousKeys.contains(key);
|
||||
}
|
||||
|
||||
public static isKeyReleasedBoth(keyA: Keys, keyB: Keys){
|
||||
return this.isKeyReleased(keyA) || this.isKeyReleased(keyB);
|
||||
}
|
||||
|
||||
private static initTouchCache() {
|
||||
this._totalTouchCount = 0;
|
||||
this._touchIndex = 0;
|
||||
47
source/src/Utils/Input/KeyboardUtils.ts
Normal file
47
source/src/Utils/Input/KeyboardUtils.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import Keys = es.Keys;
|
||||
|
||||
class KeyboardUtils {
|
||||
/**
|
||||
* 当前帧按键状态
|
||||
*/
|
||||
public static currentKeys: Keys[] = [];
|
||||
/**
|
||||
* 上一帧按键状态
|
||||
*/
|
||||
public static previousKeys: Keys[] = [];
|
||||
private static keyStatusKeys: Keys[] = [];
|
||||
|
||||
public static init(): void {
|
||||
document.addEventListener("keydown", KeyboardUtils.onKeyDownHandler);
|
||||
document.addEventListener("keyup", KeyboardUtils.onKeyUpHandler);
|
||||
}
|
||||
|
||||
public static update(){
|
||||
KeyboardUtils.previousKeys.length = 0;
|
||||
for (let key of KeyboardUtils.currentKeys){
|
||||
KeyboardUtils.previousKeys.push(key);
|
||||
KeyboardUtils.currentKeys.remove(key);
|
||||
}
|
||||
KeyboardUtils.currentKeys.length = 0;
|
||||
for (let key of KeyboardUtils.keyStatusKeys){
|
||||
KeyboardUtils.currentKeys.push(key);
|
||||
}
|
||||
}
|
||||
|
||||
public static destroy(): void {
|
||||
KeyboardUtils.currentKeys.length = 0;
|
||||
|
||||
document.removeEventListener("keyup", KeyboardUtils.onKeyUpHandler);
|
||||
document.removeEventListener("keypress", KeyboardUtils.onKeyDownHandler);
|
||||
}
|
||||
|
||||
private static onKeyDownHandler(event: KeyboardEvent): void {
|
||||
if (!KeyboardUtils.keyStatusKeys.contains(event.keyCode))
|
||||
KeyboardUtils.keyStatusKeys.push(event.keyCode);
|
||||
}
|
||||
|
||||
private static onKeyUpHandler(event: KeyboardEvent): void {
|
||||
if (KeyboardUtils.keyStatusKeys.contains(event.keyCode))
|
||||
KeyboardUtils.keyStatusKeys.remove(event.keyCode);
|
||||
}
|
||||
}
|
||||
116
source/src/Utils/Input/Keys.ts
Normal file
116
source/src/Utils/Input/Keys.ts
Normal file
@@ -0,0 +1,116 @@
|
||||
module es {
|
||||
export enum Keys {
|
||||
none,
|
||||
back = 8,
|
||||
tab = 9,
|
||||
enter = 13,
|
||||
capsLock = 20,
|
||||
escape = 27,
|
||||
space = 32,
|
||||
pageUp = 33,
|
||||
pageDown = 34,
|
||||
end = 35,
|
||||
home = 36,
|
||||
left = 37,
|
||||
up = 38,
|
||||
right = 39,
|
||||
down = 40,
|
||||
select = 41,
|
||||
print = 42,
|
||||
execute = 43,
|
||||
printScreen = 44,
|
||||
insert = 45,
|
||||
delete = 46,
|
||||
help = 47,
|
||||
d0 = 48,
|
||||
d1 = 49,
|
||||
d2 = 50,
|
||||
d3 = 51,
|
||||
d4 = 52,
|
||||
d5 = 53,
|
||||
d6 = 54,
|
||||
d7 = 55,
|
||||
d8 = 56,
|
||||
d9 = 57,
|
||||
a = 65,
|
||||
b = 66,
|
||||
c = 67,
|
||||
d = 68,
|
||||
e = 69,
|
||||
f = 70,
|
||||
g = 71,
|
||||
h = 72,
|
||||
i = 73,
|
||||
j = 74,
|
||||
k = 75,
|
||||
l = 76,
|
||||
m = 77,
|
||||
n = 78,
|
||||
o = 79,
|
||||
p = 80,
|
||||
q = 81,
|
||||
r = 82,
|
||||
s = 83,
|
||||
t = 84,
|
||||
u = 85,
|
||||
v = 86,
|
||||
w = 87,
|
||||
x = 88,
|
||||
y = 89,
|
||||
z = 90,
|
||||
leftWindows = 91,
|
||||
rightWindows = 92,
|
||||
apps = 93,
|
||||
sleep = 95,
|
||||
numPad0 = 96,
|
||||
numPad1 = 97,
|
||||
numPad2 = 98,
|
||||
numPad3 = 99,
|
||||
numPad4 = 100,
|
||||
numPad5 = 101,
|
||||
numPad6 = 102,
|
||||
numPad7 = 103,
|
||||
numPad8 = 104,
|
||||
numPad9 = 105,
|
||||
multiply = 106,
|
||||
add = 107,
|
||||
seperator = 108,
|
||||
subtract = 109,
|
||||
decimal = 110,
|
||||
divide = 111,
|
||||
f1 = 112,
|
||||
f2 = 113,
|
||||
f3 = 114,
|
||||
f4 = 115,
|
||||
f5 = 116,
|
||||
f6 = 117,
|
||||
f7 = 118,
|
||||
f8 = 119,
|
||||
f9 = 120,
|
||||
f10 = 121,
|
||||
f11 = 122,
|
||||
f12 = 123,
|
||||
f13 = 124,
|
||||
f14 = 125,
|
||||
f15 = 126,
|
||||
f16 = 127,
|
||||
f17 = 128,
|
||||
f18 = 129,
|
||||
f19 = 130,
|
||||
f20 = 131,
|
||||
f21 = 132,
|
||||
f22 = 133,
|
||||
f23 = 134,
|
||||
f24 = 135,
|
||||
numLock = 144,
|
||||
scroll = 145,
|
||||
leftShift = 160,
|
||||
rightShift = 161,
|
||||
leftControl = 162,
|
||||
rightControl = 163,
|
||||
leftAlt = 164,
|
||||
rightAlt = 165,
|
||||
browserBack = 166,
|
||||
browserForward = 167
|
||||
}
|
||||
}
|
||||
79
source/src/Utils/Input/Virtual/VirtualAxis.ts
Normal file
79
source/src/Utils/Input/Virtual/VirtualAxis.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
///<reference path="VirtualInput.ts"/>
|
||||
///<reference path="VirtualIntegerAxis.ts"/>
|
||||
module es {
|
||||
/**
|
||||
* 用-1和1之间的浮点数表示的虚拟输入
|
||||
*/
|
||||
export class VirtualAxis extends VirtualInput {
|
||||
public nodes: VirtualAxisNode[] = [];
|
||||
|
||||
public get value() {
|
||||
for (let i = 0; i < this.nodes.length; i++) {
|
||||
let val = this.nodes[i].value;
|
||||
if (val != 0)
|
||||
return val;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
constructor(...nodes: VirtualAxisNode[]) {
|
||||
super();
|
||||
this.nodes.concat(nodes);
|
||||
}
|
||||
|
||||
public update() {
|
||||
for (let i = 0; i < this.nodes.length; i++)
|
||||
this.nodes[i].update();
|
||||
}
|
||||
}
|
||||
|
||||
export class KeyboardKeys extends VirtualAxisNode {
|
||||
public overlapBehavior: OverlapBehavior;
|
||||
public positive: Keys;
|
||||
public negative: Keys;
|
||||
public _value: number = 0;
|
||||
public _turned: boolean;
|
||||
|
||||
constructor(overlapBehavior: OverlapBehavior, negative: Keys, positive: Keys) {
|
||||
super();
|
||||
this.overlapBehavior = overlapBehavior;
|
||||
this.negative = negative;
|
||||
this.positive = positive;
|
||||
}
|
||||
|
||||
public update() {
|
||||
if (Input.isKeyDown(this.positive)) {
|
||||
if (Input.isKeyDown(this.negative)) {
|
||||
switch (this.overlapBehavior) {
|
||||
default:
|
||||
case es.OverlapBehavior.cancelOut:
|
||||
this._value = 0;
|
||||
break;
|
||||
case es.OverlapBehavior.takeNewer:
|
||||
if (!this._turned) {
|
||||
this._value *= -1;
|
||||
this._turned = true;
|
||||
}
|
||||
break;
|
||||
case es.OverlapBehavior.takeOlder:
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
this._turned = false;
|
||||
this._value = 1;
|
||||
}
|
||||
} else if (Input.isKeyDown(this.negative)) {
|
||||
this._turned = false;
|
||||
this._value = -1;
|
||||
} else {
|
||||
this._turned = false;
|
||||
this._value = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public get value(){
|
||||
return this._value;
|
||||
}
|
||||
}
|
||||
}
|
||||
134
source/src/Utils/Input/Virtual/VirtualButton.ts
Normal file
134
source/src/Utils/Input/Virtual/VirtualButton.ts
Normal file
@@ -0,0 +1,134 @@
|
||||
///<reference path="./VirtualInput.ts" />
|
||||
module es {
|
||||
/**
|
||||
* 用布尔值表示的虚拟输入。除了简单地检查当前按钮状态,
|
||||
* 您还可以询问它是否刚刚被按下或释放了这个框架。
|
||||
* 您还可以将按下的按钮存储在缓冲区中一段有限的时间,或者直到调用consumeBuffer()将其使用为止。
|
||||
*/
|
||||
export class VirtualButton extends VirtualInput{
|
||||
public nodes: Node[] = [];
|
||||
public bufferTime: number = 0;
|
||||
public firstRepeatTime: number = 0;
|
||||
public mutiRepeatTime: number = 0;
|
||||
public isRepeating: boolean;
|
||||
|
||||
public _bufferCounter: number = 0;
|
||||
public _repeatCounter: number = 0;
|
||||
public _willRepeat: boolean;
|
||||
|
||||
constructor(bufferTime: number = 0, ...nodes: Node[]){
|
||||
super();
|
||||
this.nodes = nodes;
|
||||
this.bufferTime = bufferTime;
|
||||
}
|
||||
|
||||
public setRepeat(firstRepeatTime: number, mutiRepeatTime: number = firstRepeatTime){
|
||||
this.firstRepeatTime = firstRepeatTime;
|
||||
this.mutiRepeatTime = mutiRepeatTime;
|
||||
this._willRepeat = this.firstRepeatTime > 0;
|
||||
if (!this._willRepeat)
|
||||
this.isRepeating = false;
|
||||
}
|
||||
|
||||
public update() {
|
||||
this._bufferCounter -= Time.unscaledDeltaTime;
|
||||
this.isRepeating = false;
|
||||
|
||||
let check = false;
|
||||
for (let i = 0; i < this.nodes.length; i ++){
|
||||
this.nodes[i].update();
|
||||
if (this.nodes[i].isPressed){
|
||||
this._bufferCounter = this.bufferTime;
|
||||
check = true;
|
||||
}else if(this.nodes[i].isDown){
|
||||
check = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!check){
|
||||
this._repeatCounter = 0;
|
||||
this._bufferCounter = 0;
|
||||
}else if(this._willRepeat){
|
||||
if (this._repeatCounter == 0){
|
||||
this._repeatCounter = this.firstRepeatTime;
|
||||
}else{
|
||||
this._repeatCounter -= Time.unscaledDeltaTime;
|
||||
if (this._repeatCounter <= 0){
|
||||
this.isRepeating = true;
|
||||
this._repeatCounter = this.mutiRepeatTime;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public get isDown(){
|
||||
for (let node of this.nodes){
|
||||
if (node.isDown)
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public get isPressed(){
|
||||
if (this._bufferCounter > 0 || this.isRepeating)
|
||||
return true;
|
||||
|
||||
for (let node of this.nodes){
|
||||
if (node.isPressed)
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public get isReleased(){
|
||||
for (let node of this.nodes){
|
||||
if (node.isReleased)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public consumeBuffer(){
|
||||
this._bufferCounter = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个键盘键到这个虚拟按钮
|
||||
* @param key
|
||||
*/
|
||||
public addKeyboardKey(key: Keys): VirtualButton{
|
||||
this.nodes.push(new KeyboardKey(key));
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
export abstract class Node extends VirtualInputNode {
|
||||
public abstract isDown: boolean;
|
||||
public abstract isPressed: boolean;
|
||||
public abstract isReleased: boolean;
|
||||
}
|
||||
|
||||
export class KeyboardKey extends Node {
|
||||
public key: Keys;
|
||||
|
||||
constructor(key: Keys){
|
||||
super();
|
||||
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public get isDown(){
|
||||
return Input.isKeyDown(this.key);
|
||||
}
|
||||
|
||||
public get isPressed(){
|
||||
return Input.isKeyPressed(this.key);
|
||||
}
|
||||
|
||||
public get isReleased(){
|
||||
return Input.isKeyReleased(this.key);
|
||||
}
|
||||
}
|
||||
}
|
||||
43
source/src/Utils/Input/Virtual/VirtualInput.ts
Normal file
43
source/src/Utils/Input/Virtual/VirtualInput.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
module es {
|
||||
export enum OverlapBehavior {
|
||||
/**
|
||||
* 重复的输入将导致相互抵消,并且不会记录任何输入。
|
||||
* 例如:按左箭头键,按住时按右箭头键。这将导致相互抵消。
|
||||
*/
|
||||
cancelOut,
|
||||
/**
|
||||
* 将使用找到的第一个输入
|
||||
*/
|
||||
takeOlder,
|
||||
/**
|
||||
* 将使用找到的最后一个输入
|
||||
*/
|
||||
takeNewer,
|
||||
}
|
||||
|
||||
/**
|
||||
* 虚拟按钮,其状态由其VirtualInputNodes的状态决定
|
||||
*/
|
||||
export abstract class VirtualInput {
|
||||
protected constructor() {
|
||||
Input._virtualInputs.push(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从输入系统取消虚拟输入的注册。在轮询VirtualInput之后调用这个函数
|
||||
*/
|
||||
public deregister(){
|
||||
Input._virtualInputs.remove(this);
|
||||
}
|
||||
|
||||
public abstract update();
|
||||
}
|
||||
|
||||
/**
|
||||
* 将它们添加到您的VirtualInput中,以定义它如何确定当前输入状态。
|
||||
* 例如,如果你想检查一个键盘键是否被按下,创建一个VirtualButton并添加一个VirtualButton.keyboardkey
|
||||
*/
|
||||
export abstract class VirtualInputNode {
|
||||
public update() {}
|
||||
}
|
||||
}
|
||||
44
source/src/Utils/Input/Virtual/VirtualIntegerAxis.ts
Normal file
44
source/src/Utils/Input/Virtual/VirtualIntegerAxis.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
module es {
|
||||
/**
|
||||
* 用int(-1、0或1)表示的虚拟输入。它对应的输入可以从上到下到下,
|
||||
* 如使用两个键盘键作为正/负检查。
|
||||
*/
|
||||
export class VirtualIntegerAxis extends VirtualInput {
|
||||
public nodes: VirtualAxisNode[] = [];
|
||||
|
||||
public get value(){
|
||||
for (let i = 0; i < this.nodes.length; i ++){
|
||||
let val = this.nodes[i].value;
|
||||
if (val != 0)
|
||||
return Math.sign(val);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
constructor(...nodes: VirtualAxisNode[]){
|
||||
super();
|
||||
this.nodes.concat(nodes);
|
||||
}
|
||||
|
||||
public update() {
|
||||
for (let i = 0; i < this.nodes.length; i ++)
|
||||
this.nodes[i].update();
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加键盘键来模拟这个虚拟输入的左/右或上/下
|
||||
* @param overlapBehavior
|
||||
* @param negative
|
||||
* @param positive
|
||||
*/
|
||||
public addKeyboardKeys(overlapBehavior: OverlapBehavior, negative: Keys, positive: Keys){
|
||||
this.nodes.push(new KeyboardKeys(overlapBehavior, negative, positive));
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
export abstract class VirtualAxisNode extends VirtualInputNode {
|
||||
public abstract value: number;
|
||||
}
|
||||
}
|
||||
@@ -1,225 +0,0 @@
|
||||
class KeyboardUtils {
|
||||
/**
|
||||
* 键盘事件类型
|
||||
*/
|
||||
public static TYPE_KEY_DOWN: number = 0;
|
||||
public static TYPE_KEY_UP: number = 1;
|
||||
/**
|
||||
* 键值字符串枚举
|
||||
*/
|
||||
public static A: string = "A";
|
||||
public static B: string = "B";
|
||||
public static C: string = "C";
|
||||
public static D: string = "D";
|
||||
public static E: string = "E";
|
||||
public static F: string = "F";
|
||||
public static G: string = "G";
|
||||
public static H: string = "H";
|
||||
public static I: string = "I";
|
||||
public static J: string = "J";
|
||||
public static K: string = "K";
|
||||
public static L: string = "L";
|
||||
public static M: string = "M";
|
||||
public static N: string = "N";
|
||||
public static O: string = "O";
|
||||
public static P: string = "P";
|
||||
public static Q: string = "Q";
|
||||
public static R: string = "R";
|
||||
public static S: string = "S";
|
||||
public static T: string = "T";
|
||||
public static U: string = "U";
|
||||
public static V: string = "V";
|
||||
public static W: string = "W";
|
||||
public static X: string = "X";
|
||||
public static Y: string = "Y";
|
||||
public static Z: string = "Z";
|
||||
public static ESC: string = "Esc";
|
||||
public static F1: string = "F1";
|
||||
public static F2: string = "F2";
|
||||
public static F3: string = "F3";
|
||||
public static F4: string = "F4";
|
||||
public static F5: string = "F5";
|
||||
public static F6: string = "F6";
|
||||
public static F7: string = "F7";
|
||||
public static F8: string = "F8";
|
||||
public static F9: string = "F9";
|
||||
public static F10: string = "F10";
|
||||
public static F11: string = "F11";
|
||||
public static F12: string = "F12";
|
||||
public static NUM_1: string = "1";
|
||||
public static NUM_2: string = "2";
|
||||
public static NUM_3: string = "3";
|
||||
public static NUM_4: string = "4";
|
||||
public static NUM_5: string = "5";
|
||||
public static NUM_6: string = "6";
|
||||
public static NUM_7: string = "7";
|
||||
public static NUM_8: string = "8";
|
||||
public static NUM_9: string = "9";
|
||||
public static NUM_0: string = "0";
|
||||
public static TAB: string = "Tab";
|
||||
public static CTRL: string = "Ctrl";
|
||||
public static ALT: string = "Alt";
|
||||
public static SHIFT: string = "Shift";
|
||||
public static CAPS_LOCK: string = "Caps Lock";
|
||||
public static ENTER: string = "Enter";
|
||||
public static SPACE: string = "Space";
|
||||
public static BACK_SPACE: string = "Back Space";
|
||||
public static INSERT: string = "Insert";
|
||||
public static DELETE: string = "Page Down";
|
||||
public static HOME: string = "Home";
|
||||
public static END: string = "Page Down";
|
||||
public static PAGE_UP: string = "Page Up";
|
||||
public static PAGE_DOWN: string = "Page Down";
|
||||
public static LEFT: string = "Left";
|
||||
public static RIGHT: string = "Right";
|
||||
public static UP: string = "Up";
|
||||
public static DOWN: string = "Down";
|
||||
public static PAUSE_BREAK: string = "Pause Break";
|
||||
public static NUM_LOCK: string = "Num Lock";
|
||||
public static SCROLL_LOCK: string = "Scroll Lock";
|
||||
public static WINDOWS: string = "Windows";
|
||||
//存放按下注册数据的字典
|
||||
private static keyDownDict: Object;
|
||||
//存放按起注册数据的字典
|
||||
private static keyUpDict: Object;
|
||||
|
||||
public static init(): void {
|
||||
KeyboardUtils.keyDownDict = {};
|
||||
KeyboardUtils.keyUpDict = {};
|
||||
document.addEventListener("keydown", KeyboardUtils.onKeyDonwHander);
|
||||
document.addEventListener("keyup", KeyboardUtils.onKeyUpHander);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册按键
|
||||
* @param key 键值
|
||||
* @param fun 回调方法
|
||||
* @param type 按键类型 TYPE_KEY_DOWN、TYPE_KEY_UP
|
||||
*/
|
||||
public static registerKey(key: string, fun: Function, thisObj: any, type: number = 0, ...args): void {
|
||||
var keyDict: Object = type ? this.keyUpDict : this.keyDownDict;
|
||||
keyDict[key] = {"fun": fun, args: args, "thisObj": thisObj};
|
||||
}
|
||||
|
||||
/**
|
||||
* 注销按键
|
||||
* @param key 键值
|
||||
* @param type 注销的类型
|
||||
*/
|
||||
public static unregisterKey(key: string, type: number = 0): void {
|
||||
var keyDict: Object = type ? this.keyUpDict : this.keyDownDict;
|
||||
delete keyDict[key];
|
||||
}
|
||||
|
||||
/**
|
||||
* 销毁方法
|
||||
*/
|
||||
public static destroy(): void {
|
||||
KeyboardUtils.keyDownDict = null;
|
||||
KeyboardUtils.keyUpDict = null;
|
||||
document.removeEventListener("keydown", this.onKeyDonwHander);
|
||||
document.removeEventListener("keyup", this.onKeyUpHander);
|
||||
}
|
||||
|
||||
private static onKeyDonwHander(event: KeyboardEvent): void {
|
||||
if (!KeyboardUtils.keyDownDict) return;
|
||||
var key: string = KeyboardUtils.keyCodeToString(event.keyCode);
|
||||
var o: Object = KeyboardUtils.keyDownDict[key];
|
||||
if (o) {
|
||||
var fun: Function = o["fun"];
|
||||
var thisObj: any = o["thisObj"];
|
||||
var args: any = o["args"];
|
||||
fun.apply(thisObj, args);
|
||||
}
|
||||
}
|
||||
|
||||
private static onKeyUpHander(event: KeyboardEvent): void {
|
||||
if (!KeyboardUtils.keyUpDict) return;
|
||||
var key: string = KeyboardUtils.keyCodeToString(event.keyCode);
|
||||
var o: Object = KeyboardUtils.keyUpDict[key];
|
||||
if (o) {
|
||||
var fun: Function = o["fun"];
|
||||
var thisObj: any = o["thisObj"];
|
||||
var args: any = o["args"];
|
||||
fun.apply(thisObj, args);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据keyCode或charCode获取相应的字符串代号
|
||||
* @param keyCode
|
||||
* @return 键盘所指字符串代号
|
||||
*/
|
||||
private static keyCodeToString(keyCode: number): string {
|
||||
switch (keyCode) {
|
||||
case 8:
|
||||
return this.BACK_SPACE;
|
||||
case 9:
|
||||
return this.TAB;
|
||||
case 13:
|
||||
return this.ENTER;
|
||||
case 16:
|
||||
return this.SHIFT;
|
||||
case 17:
|
||||
return this.CTRL;
|
||||
case 19:
|
||||
return this.PAUSE_BREAK;
|
||||
case 20:
|
||||
return this.CAPS_LOCK;
|
||||
case 27:
|
||||
return this.ESC;
|
||||
case 32:
|
||||
return this.SPACE;
|
||||
case 33:
|
||||
return this.PAGE_UP;
|
||||
case 34:
|
||||
return this.PAGE_DOWN;
|
||||
case 35:
|
||||
return this.END;
|
||||
case 36:
|
||||
return this.HOME;
|
||||
case 37:
|
||||
return this.LEFT;
|
||||
case 38:
|
||||
return this.UP;
|
||||
case 39:
|
||||
return this.RIGHT;
|
||||
case 40:
|
||||
return this.DOWN;
|
||||
case 45:
|
||||
return this.INSERT;
|
||||
case 46:
|
||||
return this.DELETE;
|
||||
case 91:
|
||||
return this.WINDOWS;
|
||||
case 112:
|
||||
return this.F1;
|
||||
case 113:
|
||||
return this.F2;
|
||||
case 114:
|
||||
return this.F3;
|
||||
case 115:
|
||||
return this.F4;
|
||||
case 116:
|
||||
return this.F5;
|
||||
case 117:
|
||||
return this.F6;
|
||||
case 118:
|
||||
return this.F7;
|
||||
case 119:
|
||||
return this.F8;
|
||||
case 120:
|
||||
return this.F9;
|
||||
case 122:
|
||||
return this.F11;
|
||||
case 123:
|
||||
return this.F12;
|
||||
case 144:
|
||||
return this.NUM_LOCK;
|
||||
case 145:
|
||||
return this.SCROLL_LOCK;
|
||||
default:
|
||||
return String.fromCharCode(keyCode);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user