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

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

问题描述

由于某种原因,我定义的事件侦听器似乎没有收到任何事件,尽管我认为它应该。以下是我使用的MXML代码的简短说明:

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或根VBox,处理程序接收事件就行了。但是,如果我将侦听器添加到Canvas或第一个标签VBox中,则处理程序似乎没有收到任何内容。 (我假设这里只是点击Canvas区域给出了它的重点 - 我是否正确?)

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!

推荐答案

同意Ryan,我补充说,我有时会发现它有助于处理键盘事件通常,将我的听众连接到事件的捕获阶段,而不是目标或冒泡阶段(注意第三个参数,默认为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);

思考一个休闲游戏的例子,其中箭头键控制核心动作(Tetris,也许 - 向左旋转,向右旋转),在捕获阶段期间响应事件可以具有明显的优点。在文档中:

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
从根祖先开始,
继续向下显示列表到目标的
直接祖先。在大多数
的情况下,根祖先是
Stage ,然后是SystemManager,而
则是Application对象。

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.

事件传播阶段是这里更详细地描述。检查出来 - 在那里有很好的信息是值得了解的。

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处理Canvas内的键盘事件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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