Senocular, how is it that your Key.isDown code works?

senocular, I have been using your “Key.as” for quite a while now and I just want to express my gratification here.

But there is something about this class that bogs me and I have decided to express my confusion as to why it works here, I hope you’ll be able to enlighten me:

As you know, this is your code:


// Key.as
package {
   
    import flash.display.Stage;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
   
    /**
     * Usage:
     *import Key;
     * Key.initialize(stage);
     * if (Key.isDown(Keyboard.LEFT)) {  //Can be keyCode as well
     *    // Left key is being pressed
     * }
     */
    public class Key {
       
        private static var initialized:Boolean = false;  // marks whether or not the class has been initialized
        private static var keysDown:Object = new Object();  // stores key codes of all keys pressed
       
        /**
         * Initializes the key class creating assigning event
         * handlers to capture necessary key events from the stage
         */
        public static function initialize(stage:Stage) {
            if (!initialized) {
                // assign listeners for key presses and deactivation of the player
                stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
                stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
                stage.addEventListener(Event.DEACTIVATE, clearKeys);
               
                // mark initialization as true so redundant
                // calls do not reassign the event handlers
                initialized = true;
            }
        }
       
        /**
         * Returns true or false if the key represented by the
         * keyCode passed is being pressed
         */
        public static function isDown(keyCode:uint):Boolean {
            if (!initialized) {
                // throw an error if isDown is used
                // prior to Key class initialization
                throw new Error("Key class has yet been initialized.");
            }
            return Boolean(keyCode in keysDown);
        }
       
        /**
         * Event handler for capturing keys being pressed
         */
        private static function keyPressed(event:KeyboardEvent):void {
            // create a property in keysDown with the name of the keyCode
            keysDown[event.keyCode] = true;
        }
       
        /**
         * Event handler for capturing keys being released
         */
        private static function keyReleased(event:KeyboardEvent):void {
            if (event.keyCode in keysDown) {
                // delete the property in keysDown if it exists
                delete keysDown[event.keyCode];
            }
        }
       
        /**
         * Event handler for Flash Player deactivation
         */
        private static function clearKeys(event:Event):void {
            // clear all keys in keysDown since the player cannot
            // detect keys being pressed or released when not focused
            keysDown = new Object();
        }
    }
}

Here’s my question:


 private static function keyPressed(event:KeyboardEvent):void {
            // create a property in keysDown with the name of the keyCode
            keysDown[event.keyCode] = true;
        }

Now I have no problem with this, perfectly normal here, the keysDown now holds a Number.
Actionscript, upon seeing that you assign a “true” to the keyCode number inside keysDown, make the Number inside the keysDown object an Object as well.

So essentially, we have, a Boolean [true], inside an Object [whatever the keyCode is], inside an Object [keysDown].

It could also be said that keysDown now holds an array of Objects that holds Booleans.

Makes perfect sense.

Ok here’s the troubling bit, to me, it seems almost magical that it works:


private static function keyReleased(event:KeyboardEvent):void {
            if (event.keyCode in keysDown) {
                // delete the property in keysDown if it exists
                delete keysDown[event.keyCode];
            }

One is essentially asking Actionscript, if this number[event.keyCode] in this Object[keysDown] is true…

My mind just go “What ?!!?”.
Because…how is it that a number contains a true ?
[I understand that anything other than zero is true but we are not talking about native/primitive typecast translation here]
event.keyCode IS a numeric figure, not an object.

So…it’s almost AMAZING that Actionscript “thinks”: “ok, programmer is asking if number in an object contains an object…but number cannot contain an object…maybe that number is not a primitive but an object, that’s the only way it can hold another object, I shall now internally typecast the event.keyCode to make it into an object and proceed from there”.

As far as my limited understanding goes, this is the only reason why your code works, and if I am correct, Actionscript is a very amazing…almost A.I. like language!

I know I am probably wrong, please enlighten me as to why your code works :slight_smile:
Thank you for explaining yourself to this retard.