tags:

views:

136

answers:

1

Hi guys..

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

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

+2  A: 

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
    }
}
Arkadiusz Kondas
Yes, thanks, that works! Two things:1) I couldn't add the event listeners to 'this.stage' since it is null, but adding to 'this' works2) Right after application startup I had to click a component to give focus to the Flex App, otherwise I wouldn't get any keyboard events. Just clicking anywhere into the Flex App did not do the trick.
Tom
I had a similar problem with a List and the itemClickEvent, which also doesn't contain key modifier info. Arkadiusz Kondas' solution helped.
Maurits de Boer