﻿//****************************************************************************
//Copyright © 2005-2009. Adobe Macromedia Software LLC. All rights reserved.
//The following Code is subject to all restrictions 
//contained in the End User License Agreement accompanying
//this product.
//****************************************************************************
package com.adobe.captivate.flash
{
    import flash.display.Stage;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
   
    public class rdKey
	{
        private static var m_initialized:Boolean = false;
        private static var m_keysDown:Object = new Object();
       
		 function rdKey()
		 {
		 }
		 
        public static function initialize(stage:Stage):void
		{
            if(!m_initialized)
			{
                stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
                stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
                stage.addEventListener(Event.DEACTIVATE, clearKeys);  
                m_initialized = true;
            }
        }
       
        public static function isDown(keyCode:uint):Boolean
		{
			if(!m_initialized)
			{
				throw new Error("Key class not initialized.");
            }
            return Boolean(keyCode in m_keysDown);
        }
       
        private static function keyPressed(event:KeyboardEvent):void
		{
            m_keysDown[event.keyCode] = true;
        }
       
        private static function keyReleased(event:KeyboardEvent):void
		{
            if (event.keyCode in m_keysDown)
			{
                delete m_keysDown[event.keyCode];
            }
        }
       
        public static function clearKeys(event:Event):void
		{
            m_keysDown = new Object();	//old object will be garbage collected
        }
    }
}