Search code examples
apache-flexflex4

Cancel drag on esc button


I have a list control with enabled dragging in it. How could I cancel dragging by pressing Esc key?


Solution

  • There is no default behavior for cancelling of dragging, but you can listen for keyboard and simulate the mouse up event. Here is example (The solution is taken from here):

    <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
                   xmlns:s="library://ns.adobe.com/flex/spark" 
                   xmlns:mx="library://ns.adobe.com/flex/mx"
                   keyDown="onKeyDown(event)">
    
            <s:List  dataProvider="{dataProvider}"  dragEnabled="true" />
    
        <fx:Script>
            <![CDATA[
                import mx.collections.ArrayCollection;
                import mx.managers.DragManager;
                import mx.managers.dragClasses.DragProxy;
    
                [Bindable]
                public var dataProvider:ArrayCollection = new ArrayCollection([{label: "name1"}, {label: "name2"}, {label: "name3"}, {label: "name4"}]);
    
                private function onKeyDown(event:KeyboardEvent):void
                {
                    if (event.keyCode == Keyboard.ESCAPE)
                    {
                        DragManager.acceptDragDrop(null);
    
                        // get drag proxy
                        var dragProxy: DragProxy = DragManager.mx_internal::dragProxy;
                        if (dragProxy != null) {
                            //provide some thing for mouse up
                            dragProxy.mouseUpHandler(new MouseEvent(MouseEvent.MOUSE_UP));
                        }
                    }
                }
            ]]>
        </fx:Script>
    </s:Application>