MouseEvent shiftKey or ctrlKey is always false

I just add the Flex HSlider to my dialog. This allows you to set the length of time in days, and I thought it would be great if the user could snap to weeks by pressing SHIFT while dragging the slider.

Unfortunately, the event passed to the event handler does not contain information about the key modifier.

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 buttonDown=false delta=0]

      

Has anyone figured out how to tell if SHIFT (or CTRL) was pressed while dragging? Thank!

+2


source to share


2 answers


My suggestion:

Add this variable to your application:

private var shiftPressed:Boolena = false;

      

Add this line to createComplete handler in your application

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

      



Then add these functions

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

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

      

And change your function

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

      

+2


source


I found this solution:

set the shiftPressed variable as mentioned earlier:

private var shiftPressed:Boolean = false;

      

then use the predefined listeners in the component s:slider

:



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

      

then you can use the property snapInterval

for s:slider

or implement the logic in the handler change

, in my case I just needed to step 45 if the shift was pressed and 0.01 otherwise, so I used:

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

      

0


source







All Articles