actionscript-3flashapache-flexmouseeventmodifier-key

Flex: Determine if Space key is held down during MouseEvent


The MouseEvent class has the properties altKey, ctrlKey and shiftKey that I can use to determine if a modifier key (namely, Alt, Ctrl and Shift) was held down when the event occurred.

But I want to determine if Space key is held down during a MouseEvent. How can I do this?


Solution

  • As @Timofei Davydik suggested, I do this the "manual" way by listening to KeyboardEvent and set a flag to indicate the state of space key being held. Then I just use the value of this flag in the listener of MouseEvent. The code is somewhat like this (you can add any other key that you need to watch):

    The KeyboardEvent listeners:

    public class ModifierKeyboard
    {
        /**
         *determine whether space key is held down
         */
        public static var spaceIsHeld:Boolean = false;
    
        /**
         * this handles the keyDown event on main app
         */
        public static function app_keyDownHandler(event:KeyboardEvent):void
        {
            switch (event.keyCode)
            {
                case Keyboard.SPACE:
                    if (!spaceIsHeld)
                    {
                        spaceIsHeld = true;
                    }
                    break;
            }
        }
    
        /**
         * this handles the keyUp event on main app
         */
        public static function app_keyUpHandler(event:KeyboardEvent):void
        {
            switch (event.keyCode)
            {           
                case Keyboard.SPACE:
                    spaceIsHeld = false;
                    break;
            }
        }
    }