mouseUp 和 mouseMove 在应用程序之外?

发布于 2024-11-14 07:46:46 字数 1866 浏览 2 评论 0原文

我正在尝试实现一种非常简单的方法来通过鼠标选择屏幕的一部分。该工作流程是许多应用程序的标准工作流程 - 单击起点,移动鼠标,然后在单击的第一个点和鼠标当前位置之间更新透明矩形。基本代码看起来像这样(减去图形,这很简单)

<?xml version="1.0" encoding="utf-8"?>
<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" minWidth="955" minHeight="600" mouseDown="application1_mouseDownHandler(event)">

    <fx:Script>
        <![CDATA[
            import spark.components.mediaClasses.VolumeBar;

            private var _anchorPoint:Point = new Point();
            private var _currentPoint:Point = new Point();

            protected function application1_mouseDownHandler(event:MouseEvent):void
            {
                addEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove);
                addEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
                _anchorPoint = new Point(event.stageX, event.stageY);   
            }

            private function handleMouseMove(e:MouseEvent):void
            {
                _currentPoint.x = e.stageX;
                _currentPoint.y = e.stageY;
                trace("rectangle between (",_anchorPoint.x, ",", _anchorPoint.y, ") and (", _currentPoint.x, ",", _currentPoint.y, ").");
            }

            private function handleMouseUp(e:MouseEvent):void
            {
                removeEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove);
                removeEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
            }
        ]]>
    </fx:Script>
</s:Application>

当用户将鼠标移到应用程序之外时,它就会崩溃。不仅 _currentPoint 停止更新,而且如果您在应用程序外部放开鼠标按钮,您会错过 mouseUp 事件,即当您将鼠标移回到应用程序上时 _currentPoint 再次开始更新,就好像您从未放开鼠标按钮一样。想知道Flex(对于网络应用程序)是否有一种方法可以通过在应用程序外部监听 mouseMove 和 mouseUp 事件(如果可能的话)或任何其他可能有意义的方式来解决这个问题。

感谢您的帮助!

I'm trying to implement a very simple way to select a subsection of the screen via mouse. The workflow is the standard one for many apps - click on starting point, move mouse and transparent rectangle updates between first point clicked and current position of the mouse. The basic code looks something like this (minus the graphics, which is simple)

<?xml version="1.0" encoding="utf-8"?>
<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" minWidth="955" minHeight="600" mouseDown="application1_mouseDownHandler(event)">

    <fx:Script>
        <![CDATA[
            import spark.components.mediaClasses.VolumeBar;

            private var _anchorPoint:Point = new Point();
            private var _currentPoint:Point = new Point();

            protected function application1_mouseDownHandler(event:MouseEvent):void
            {
                addEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove);
                addEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
                _anchorPoint = new Point(event.stageX, event.stageY);   
            }

            private function handleMouseMove(e:MouseEvent):void
            {
                _currentPoint.x = e.stageX;
                _currentPoint.y = e.stageY;
                trace("rectangle between (",_anchorPoint.x, ",", _anchorPoint.y, ") and (", _currentPoint.x, ",", _currentPoint.y, ").");
            }

            private function handleMouseUp(e:MouseEvent):void
            {
                removeEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove);
                removeEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
            }
        ]]>
    </fx:Script>
</s:Application>

This breaks down when the user moves the mouse outside of the app. Not only _currentPoint stops updating, but also if you let go the mouse button outside of the app you miss the mouseUp event, i.e. when you move the mouse back on the app _currentPoint starts updating again as if you had never let go of the mouse button. Was wondering if there is a way in Flex (for web apps) to get around this by listening to mouseMove and mouseUp events when outside of the app (if that's possible) or whatever other way that may make sense.

thank you for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

西瓜 2024-11-21 07:46:46

大多数人不知道的是:如果 MOUSE_DOWN 事件被触发,则 MouseEvents 会在应用程序窗口之外被跟踪,但 MOUSE_UP 事件不会被触发。您可以抓取应用程序窗口之外(甚至浏览器窗口之外)的鼠标位置只要您所做的任何事情都会让用户按住鼠标。要对此进行测试,请尝试运行以下命令代码:

<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"
           creationComplete="init()">
<fx:Script>
    <![CDATA[
        protected function init():void {
            addEventListener(Event.ADDED_TO_STAGE, magic)
        }

        protected function magic(e:Event):void {
            stage.addEventListener(MouseEvent.MOUSE_MOVE, moreMagic);
        }

        protected function moreMagic(e:MouseEvent):void {
            magicalButton.label = "Hold me down! " + String(e.stageX) + "x" + String(e.stageY);
        }
    ]]>
</fx:Script>    
<s:Button id="magicalButton" label="Hold me down!"/>

Here's something most people don't know: MouseEvents are tracked outside of the application window if the MOUSE_DOWN event has been fired, but not MOUSE_UP. You can grab mouse positions outside of the application window (and even outside of the browser window) as long as whatever you're doing makes the user hold their mouse down. To test this, try running the following code:

<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"
           creationComplete="init()">
<fx:Script>
    <![CDATA[
        protected function init():void {
            addEventListener(Event.ADDED_TO_STAGE, magic)
        }

        protected function magic(e:Event):void {
            stage.addEventListener(MouseEvent.MOUSE_MOVE, moreMagic);
        }

        protected function moreMagic(e:MouseEvent):void {
            magicalButton.label = "Hold me down! " + String(e.stageX) + "x" + String(e.stageY);
        }
    ]]>
</fx:Script>    
<s:Button id="magicalButton" label="Hold me down!"/>

送舟行 2024-11-21 07:46:46

在 AIR 中进行一些黑客攻击也许可以实现这一点,但在浏览器中打开 Web 应用程序绝对不行。

想象一下,如果网站可以在浏览器之外跟踪您的鼠标并能够截取您的操作系统的屏幕截图,会发生什么!

It might be possible with some hacking in AIR, but definitely not with a webapp opened in a browser.

Just imagine what would happen if websites could track your mouse outside the browser and be able to take screenshots of your OS!

把梦留给海 2024-11-21 07:46:46

虽然这不会让您到达您想要的位置,但您可以使用 Event.MOUSE_LEAVEMouseEvent.MOUSE_MOVE 事件来跟踪光标是否在鼠标的边界内。应用。

Though this won't get you to where you want, you can make use of Event.MOUSE_LEAVE and MouseEvent.MOUSE_MOVE events to track whether the cursor is within the boundaries of the application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文