开发者

MouseEvent shiftKey or ctrlKey is always false

开发者 https://www.devze.com 2022-12-09 16:29 出处:网络
I am just adding a Flex HSlider to my dialog. It allows for setting a time duration in days, and I thought it would be great if the user can snap to weeks by pressing the SHIFT key while dragging the

I am just adding a Flex HSlider to my dialog. It allows for setting a time duration in days, and I thought it would be great if the user can snap to weeks by pressing the SHIFT key while dragging the slider.

Unfortunately the event passed to the event-handler contains no key modifier information..

Here is my code:

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

which produces the following output:

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 buttonD开发者_如何学JAVAown=false delta=0]

Anybody got an idea how to find out if SHIFT (or CTRL) was pressed while dragging? Thanks!


My proposition:

Add this variable in your application:

private var shiftPressed:Boolena = false;

Add this line in creationComplete handler in your application

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

Next add this functions

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

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

And modify your function

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


i've found this solution:

set the shiftPressed variable as said before:

private var shiftPressed:Boolean = false;

then use the predefined listeners on the s:slider component:

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

then you can use the snapInterval property of the s:slider or implement the logic into the change handler, in my case i needed just to step by 45 if the shift was pressed and by 0.01 otherwise, so i used:

snapInterval="{(shiftPressed)?45:0.01}"
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号