Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package {
  2.     import flash.display.Stage;
  3.     import flash.events.Event;
  4.     import flash.events.KeyboardEvent;
  5.    
  6.     public class Key {
  7.         private static var initialized:Boolean = false;
  8.         private static var keysDown:Object = new Object();
  9.        
  10.         public static function initialize(stage:Stage) {
  11.             if (!initialized) {
  12.                 stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
  13.                 stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
  14.                 stage.addEventListener(Event.DEACTIVATE, clearKeys);
  15.                
  16.                 initialized = true;
  17.             }
  18.         }
  19.        
  20.         public static function isDown(keyCode:int):Boolean {
  21.             if (!initialized) {
  22.                 throw new Error("Key class has yet been initialized.");
  23.             }
  24.             return Boolean(keyCode in keysDown);
  25.         }
  26.        
  27.         private static function keyPressed(event:KeyboardEvent):void {
  28.             keysDown[event.keyCode] = true;
  29.         }
  30.        
  31.         private static function keyReleased(event:KeyboardEvent):void {
  32.             if (event.keyCode in keysDown) {
  33.                 delete keysDown[event.keyCode];
  34.             }
  35.         }
  36.        
  37.         private static function clearKeys(event:Event):void {
  38.             keysDown = new Object();
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement