与处理使用Flex画布内部键盘事件问题 [英] Problem with handling keyboard events inside a Canvas using Flex

查看:187
本文介绍了与处理使用Flex画布内部键盘事件问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某些原因,该事件侦听器我定义似乎永远不会接收任何事件,但我相信它应该。下面是我使用的MXML code一个非常简短的描述:

For some reason, the event listener I define never seems to receive any events, although I believe it should. Here's a very short description of the MXML code I'm using:

WindowedApplication
    VBox (root box)
        MenuBar
        TabNavigator
            VBox (first tab)
                Canvas
            VBox (second tab)

如果我添加一个监听器KEY_DOWN事件的WindowedApplication或根垂直框,处理程序接收事件就好了。但是,如果我的监听器添加到画布或第一个选项卡垂直框,处理程序似乎从来没有收到任何。 (我这里假设只是点击画布的区域给予其焦点 - 我是正确的?)

If I add a listener for KEY_DOWN events to WindowedApplication or the root VBox, the handler receives the events just fine. But if I add the listener to the Canvas or the first tab VBox, the handler never seems to receive any. (I assume here that just clicking on the Canvas area gives it focus - am I correct?)

我刚刚开始使用Flex,所以我希望我的地方做了一个愚蠢的初学者的错误。我会非常感谢任何帮助。谢谢!

I'm just starting out with Flex, so I'm hoping I made a silly beginner's mistake somewhere. I'd be very grateful for any help. Thanks!

推荐答案

同意瑞安,和我想补充一点,我有时会发现它有帮助的,在一般情况处理键盘事件的时候,给我的听众连线到事件的捕捉的阶段,而不是目标或冒泡阶段(注意第三个参数,默认为false):

Agree with Ryan, and I'd add that I've sometimes found it helpful, when handling keyboard events in general, to wire up my listeners to the event's capture phase, rather than the target or bubbling phases (note the third argument, false by default):

stage.addEventListener(KeyboardEvent.KEY_DOWN, handleKeyDown, true);

思考了休闲游戏,方向键不知怎么控制核心作用的实例(俄罗斯方块,也许 - 向左旋转,向右旋转),捕获阶段是指在响应一个事件可以有明显的优势。从文档:

Thinking for instance of a casual game in which the arrow keys control the core action somehow (Tetris, maybe -- rotate left, rotate right), responding to an event during the capture phase means can have distinct advantages. From the docs:

在捕获阶段,Flex的检   在显示事件的祖先   列表,看看哪些是注册   作为一个监听事件。 Flex的   开始于根祖先和   继续向下显示列表中   直接祖先的目标。在大多数   的情况下,根祖先是   舞台,然后以SystemManager,和   那么App​​lication对象。

In the capturing phase, Flex examines an event's ancestors in the display list to see which ones are registered as a listener for the event. Flex starts with the root ancestor and continues down the display list to the direct ancestor of the target. In most cases, the root ancestors are the Stage, then the SystemManager, and then the Application object.

因此​​,在这种情况下,你可以肯定在舞台的监听器将得到通知第一,在其他人之前,并可以做出相应的反应 - 无论是完全终止事件传播(有时你可能会想这样做)或将其转化为一个更特定应用程序,自定义事件:

So in this case, you can be sure the stage's listener will get the notification first, before anyone else, and can respond accordingly -- either by terminating the event-propagation entirely (sometimes you might want to do that) or by translating it into a more application-specific, custom event:

private function handleKeyDown(event:KeyboardEvent):void
{
    if (event.keyCode == Keyboard.LEFT)
    {
    	dispatchEvent(new Event("rotateLeft"));
    }
    else if (event.keyCode == Keyboard.RIGHT)
    {
    	dispatchEvent(new Event("rotateRight"));
    }
    else
    {
    	event.stopPropagation();
    }
}

...和你保持距离胡椒键盘监听器在你的应用程序。

... and keeping you from peppering keyboard listeners all over your app.

事件传播阶段是更详细的描述rel="nofollow">。检查出来,以及 - 有一个在那里,很值得了解伟大的信息

Event-propagation phases are described in more detail here. Check it out as well -- there's great info in there that's well worth knowing.

这篇关于与处理使用Flex画布内部键盘事件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆