取消拖动esc按钮

时间:2013-02-04 07:23:37

标签: flex flex4

我有一个列表控件,启用了拖动功能。如何通过按 Esc 键取消拖动?

1 个答案:

答案 0 :(得分:2)

取消拖动没有默认行为,但您可以侦听键盘并模拟鼠标注册事件。以下是示例(解决方案取自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>