简体   繁体   English

有谁知道如何在 Flex/Flash Air 应用程序中捕获鼠标?

[英]Does anyone know how to capture the mouse in Flex/Flash Air application?

Does someone know how to capture the mouse in an air application.有人知道如何在空中应用程序中捕获鼠标。 I know its possible because the flex scrollbar captures the mouse.我知道这是可能的,因为 flex 滚动条会捕获鼠标。 I want to replicate the scrollbar's mouse capture.我想复制滚动条的鼠标捕获。

Try this in a flex/air application window:在 flex/air 应用程序 window 中试试这个:

<s:Scroller height="500" width="300">
    <s:VGroup>
        <s:Rect width="100%" height="2000">
            <s:fill>
                <s:SolidColor color="#ffcc00"/>
            </s:fill>
        </s:Rect>
    </s:VGroup>
</s:Scroller>

If you press and hold the scrollbar handle and drag outside, even outside the window, the scrollbar still works.如果按住滚动条手柄并向外拖动,即使在 window 之外,滚动条仍然有效。 It wont lose focus.它不会失去焦点。 So it is capturing the mouse.所以它正在捕获鼠标。

what I want:我想要的是:

  • Mouse and touch capture.鼠标和触摸捕获。 (Even outside the window while still pressing down the button or touch point) (即使在 window 之外,仍然按下按钮或触摸点)

Here's some code I use for this purpose:这是我用于此目的的一些代码:

import flash.display.InteractiveObject;
import flash.events.MouseEvent;
import flash.geom.Point;

public class Dragger
{
    private var target:InteractiveObject;
    private var mouseDownPoint:Point;
    private var originalPosition:Point;

    public function Dragger(target:InteractiveObject)
    {
        this.target = target;
        target.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
    }

    private function onMouseDown(event:MouseEvent):void
    {
        mouseDownPoint = new Point(event.stageX, event.stageY);
        originalPosition = new Point(target.x, target.y);
        event.stopPropagation();

        target.stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
        target.stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
    } 
    private function onMouseUp(event:MouseEvent):void
    {
        onMouseMove(event);
        mouseDownPoint = null;
        event.stopPropagation();
        target.stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
        target.stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
    } 
    private function onMouseMove(event:MouseEvent):void
    {
        if (!mouseDownPoint) return;
        target.x = originalPosition.x + event.stageX - mouseDownPoint.x;
        target.y = originalPosition.y + event.stageY - mouseDownPoint.y;
        event.updateAfterEvent();               
        event.stopPropagation();
    } 
}

To use, simply do new Dragger(your_object).要使用,只需执行 new Dragger(your_object)。

At least on desktops using a mouse, this should Just Work as far as I know.至少在使用鼠标的桌面上,据我所知,这应该可以正常工作。 For example:例如:

import flash.text.TextField;
import flash.events.MouseEvent;

var t:TextField = new TextField();
stage.addChild(t);
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouse);

function onMouse( e:MouseEvent ) {
    t.text = e.stageX +", "+ e.stageY;
}

For me, events show up in the text field both when the mouse is inside the Flash area, and when you click inside Flash and drag outside.对我来说,当鼠标在 Flash 区域内以及在 Flash 内单击并拖动到外部时,事件都会显示在文本字段中。 So if you're having problems, my guess is that it might be related to the components you're using or how your content is structured (eg what object you're registering for events on, etc.).因此,如果您遇到问题,我的猜测是它可能与您正在使用的组件或您的内容的结构方式有关(例如,您正在注册的事件是什么 object 等)。 But there isn't any special way of getting mouse events to continue when the user drags out of the Flash rectangle.但是当用户拖出 Flash 矩形时,没有任何特殊的方法可以让鼠标事件继续。

(Note that touch devices may be a different situation - often their browsers eat pan gestures before they get to Flash except in fullscreen mode, or sometimes they give Flash drag events when Flash is "zoomed in" (by double-tapping). Your mileage may vary with different browsers on different devices..) (请注意,触摸设备可能是一种不同的情况 - 通常他们的浏览器在到达 Flash 之前会吃平移手势,除非在全屏模式下,或者有时当 ZBF2C44E6FC09515648B.91B1BB4EC3F5FZ 出现时,他们会给出 Flash 拖动事件(“你的双倍里程窃听”)可能因不同设备上的不同浏览器而异..)

You can't get any information that would be meaningful for you.您无法获得任何对您有意义的信息。 I understand what you're trying to accomplish, and yes, you're right that scroll bars have some black magic to them in that if you begin a drag it will continue to scroll even if the mouse is out of the range of the player.我了解您要完成的工作,是的,您是对的,滚动条对它们有一些魔力,因为如果您开始拖动,即使鼠标超出播放器范围,它也会继续滚动.

I ran some tests on trying to listen into events and came up empty handed.我对尝试监听事件进行了一些测试,结果空手而归。 The player itself must get them but they aren't exposed in anyway that I can find.玩家自己必须得到它们,但它们不会以我能找到的方式暴露出来。

So, I thought about creating a more or less hidden scroll component (or set of them) that would capture the click to and start the drag event.所以,我考虑创建一个或多或少隐藏的滚动组件(或一组)来捕获点击并启动拖动事件。 Somewhat a hack, but cool idea...有点黑客,但很酷的想法......

Then you could poll the current scroll position to make a guess at where the mouse is (outside of the window) with a set of horiz and vert scrollers.然后,您可以轮询当前滚动 position 以使用一组水平和垂直滚动来猜测鼠标的位置(窗口外)。 You CAN do this, however, the issue comes that the scroller position values will have maximum limits based on their native size.您可以这样做,但是,问题在于滚动条 position 值将根据其原始大小具有最大限制。 This means that you'll be left with gaps on where the mouse is.这意味着您将在鼠标所在的位置留有空隙。 So you could knowhow far up or down the Y axis mouse is positioned on the screen, even if it is outside far the the left or right (X axis) of the appliction (I was on a second monitor for example), BUT you will not know where it exists on the X axis, and you would only know the Y as it relates to the application size (not above or below where it exists on the screen elsewhere).因此,您可以知道 Y 轴鼠标在屏幕上的位置向上或向下多远,即使它位于应用程序的左侧或右侧(X 轴)之外(例如,我在第二台显示器上),但您会不知道它在 X 轴上的位置,并且您只会知道与应用程序大小相关的 Y(不高于或低于它在屏幕上其他位置的位置)。

Here's an image of what you could capture with this trick / hack:这是您可以使用此技巧/技巧捕获的图像:在此处输入图像描述

So yes, you could do a polling type of method like I described, but, you would only get those coordinates.所以是的,你可以像我描述的那样做一个轮询类型的方法,但是,你只会得到那些坐标。

Ultimately, it isn't very helpful because you need to expand the boundries of the scroll rect (or app) to fully capture where the mouse is - thus you would expand it to the full size of the screen, and could just use normal events.最终,它不是很有帮助,因为您需要扩展滚动矩形(或应用程序)的边界以完全捕获鼠标所在的位置 - 因此您可以将其扩展为屏幕的完整尺寸,并且可以只使用普通事件. This is particularly true of the negative boundries (left or above app).对于负边界(左侧或上方的应用程序)尤其如此。

What I havent tried is creating a large container positioned outside the main app window and applying a scroller to it.我没有尝试过的是创建一个位于主应用程序 window 之外的大型容器并对其应用滚动条。 If you did this you could potentially make it work:如果您这样做,您可能会使其工作:

在此处输入图像描述

It was an interesting concept, clever - definitely not supported functionality.这是一个有趣的概念,聪明 - 绝对不支持的功能。 :P~ :P~

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM