代码
package tools{
public class KeyIsDown{
private static var obj;
public static function init(stage){
obj = new Object();
stage.addEventListener("keyDown", keydownHandler);
stage.addEventListener("keyUp", keyupHandler);
}
public static function isDown(key){
return(obj[key] ? true : false);
}
private static function keydownHandler(event){
obj[event.keyCode] = true;
}
private static function keyupHandler(event){
obj[event.keyCode] = false;
}
}
}
public class KeyIsDown{
private static var obj;
public static function init(stage){
obj = new Object();
stage.addEventListener("keyDown", keydownHandler);
stage.addEventListener("keyUp", keyupHandler);
}
public static function isDown(key){
return(obj[key] ? true : false);
}
private static function keydownHandler(event){
obj[event.keyCode] = true;
}
private static function keyupHandler(event){
obj[event.keyCode] = false;
}
}
}
开始的时候在文档类构造的时候KeyIsDown.init(stage)一下
然后在任何地方KeyIsDown.isDown(keycode)都可以返回keycode有没有按下了