Flex:确定在MouseEvent期间是否按住Space键

时间:2013-09-13 11:03:08

标签: actionscript-3 flash flex mouseevent modifier-key

MouseEvent类具有altKeyctrlKeyshiftKey属性,可用于确定是否有修饰键(即 Alt Ctrl Shift )。

但我想确定在MouseEvent期间是否按下了 Space 键。我怎么能这样做?

1 个答案:

答案 0 :(得分:3)

正如@Timofei Davydik建议的那样,我通过听KeyboardEvent并以“手动”方式进行操作,并设置一个标志来指示空间键的状态。然后我只在MouseEvent的监听器中使用此标志的值。代码有点像这样(你可以添加你需要观察的任何其他键):

KeyboardEvent侦听器:

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