MouseEvent shiftKey或ctrlKey始终为false

时间:2009-10-26 09:06:58

标签: flex events

我只是在我的对话框中添加了Flex HSlider。它允许以天为单位设置持续时间,我认为如果用户可以在拖动滑块的同时按住SHIFT键来捕捉周数,那将会很棒。

不幸的是,传递给事件处理程序的事件不包含任何键修饰符信息..

这是我的代码:

protected function onDurationSliderChange (event:SliderEvent) : void
{
    var durationInDays : int = this.sld_durationInDays.value;

    // modifiers
    if (event.triggerEvent is MouseEvent) {
        var mouseEvt : MouseEvent = event.triggerEvent as MouseEvent;
        trace (mouseEvt.ctrlKey + "  " + mouseEvt.ctrlKey + "  " + event.keyCode);
        trace (mouseEvt);

        // when using SHIFT, snap to week
        if (mouseEvt.shiftKey && !mouseEvt.ctrlKey)
            durationInDays = int(durationInDays/7) * 7;
    }
    this.durationInDays = durationInDays;
}

产生以下输出:

false  false  -1
[MouseEvent type="click" bubbles=true cancelable=false eventPhase=2 localX=NaN localY=NaN stageX=NaN stageY=NaN relatedObject=null ctrlKey=false altKey=false shiftKey=false buttonDown=false delta=0]

任何人都知道如何在拖动时找出是否按下了SHIFT(或CTRL)?谢谢!

2 个答案:

答案 0 :(得分:2)

我的主张:

在您的应用程序中添加此变量:

private var shiftPressed:Boolena = false;

在应用程序的creationComplete处理程序中添加此行

this.stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeyHandler);
this.stage.addEventListener(KeyboardEvent.KEY_UP, unCheckKeyHandler);

接下来添加此功能

private function checkKeyHandler(event:KeyboardEvent):void {
    if (event.shiftKey)
        shiftPressed = true;
}

private function unCheckKeyHandler(event:KeyboardEvent):void {
    shiftPressed = false;
}

修改你的功能

protected function onDurationSliderChange (event:SliderEvent) : void
{
    if(shiftPressed) {
        //add your operations
    } else {
        //add your operations
    }
}

答案 1 :(得分:0)

我找到了这个解决方案:

设置如前所述的shiftPressed变量:

private var shiftPressed:Boolean = false;

然后使用s:slider组件上的预定义侦听器:

mouseDown="{shiftPressed = event.shiftKey}"
mouseUp="{shiftPressed = false}"

然后你可以使用snapInterval的{​​{1}}属性或者在s:slider处理程序中实现逻辑,在我的情况下,如果按下切换,我只需要步骤45否则为0.01,所以我用过:

change