Flex: Determine if Space key is held down during MouseEvent

1k views Asked by At

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?

1

There are 1 answers

0
Hoang Huynh On BEST ANSWER

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;
        }
    }
}