智慧 + 毅力 = 无所不能

正确性、健壮性、可靠性、效率、易用性、可读性、可复用性、兼容性、可移植性...

导航

stage按键监听

Posted on 2009-12-02 14:41  Bill Yuan  阅读(274)  评论(0编辑  收藏  举报
代码
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;
                }
                
        }
}

开始的时候在文档类构造的时候KeyIsDown.init(stage)一下
然后在任何地方KeyIsDown.isDown(keycode)都可以返回keycode有没有按下了