0

我有一个启用拖动的列表控件。如何通过按键取消拖动Esc

4

1 回答 1

2

取消拖动没有默认行为,但您可以监听键盘并模拟鼠标向上事件。这是示例(解决方案取自此处):

<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>
于 2013-02-04T10:13:20.573 回答