全局事件监听器,当焦点位于 texiInput 内时

发布于 2024-10-30 17:10:49 字数 306 浏览 0 评论 0原文

我正在尝试创建一个全局事件监听器。一切正常,KeyboardEvent.KEY_DOWN 一直触发..除了 textInput 具有焦点的情况。

这是我附加侦听器的方法:

FlexGlobals.topLevelApplication.systemManager.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler, true);

我已经尝试了两种方法(使用捕获和不使用捕获)。我缺少什么?

感谢您的宝贵时间:)

I'm trying to make a global eventListener. Everything works fine, the KeyboardEvent.KEY_DOWN fires all the time .. except the cases when the textInput has a focus.

Here's how I attach listener:

FlexGlobals.topLevelApplication.systemManager.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler, true);

I've tried both (use capture and without it). What am I missing?

Thanks for your time :)

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

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

发布评论

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

评论(2

扶醉桌前 2024-11-06 17:10:49

首先,我不知道为什么您使用 systemManager 作为侦听器对象,因为它不是显示列表的一部分,因此它无法获取冒泡事件。其次,使用 FlexGlobals 并不是最好的做事方式(就我个人而言,除了将它用于弹出窗口之外,我看不出有什么理由应该使用它)。

如果您想收听全球活动,只需将其放在舞台上即可。每个视图组件都有一个“stage”属性,它指向 Flex 应用程序的主阶段。试试这个:

stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);

First, I don't know why you're using the systemManager as your listener object since it's not part of the display list, hence it can't get bubbling events. Second, using FlexGlobals isn't the best way of doing things (personally, other than using it for popups, I don't see many reasons why you should use it).

If you want to listen for a global event, just put it on the stage. Every view component has a 'stage' property which points to the main stage of your flex app. Try this:

stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
哑剧 2024-11-06 17:10:49

嗯...我不太明白问题出在哪里。

这是一个简单的应用程序:

    <?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"
               keyDown="application1_keyDownHandler(event)"
               creationComplete="application1_creationCompleteHandler(event)"
               >

    <fx:Script>
        <![CDATA[
            import flash.utils.getQualifiedClassName;

            import mx.core.FlexGlobals;
            import mx.events.FlexEvent;
            protected function application1_keyDownHandler(event:KeyboardEvent):void
            {
                trace("Key Down Handler: key = " + event.keyCode);
            }

            protected function application1_creationCompleteHandler(event:FlexEvent):void
            {
                FlexGlobals.topLevelApplication.addEventListener(KeyboardEvent.KEY_DOWN, tlaKeyDownHandler);
                FlexGlobals.topLevelApplication.systemManager.addEventListener(KeyboardEvent.KEY_DOWN, smKeyDownHandler);
            }

            protected function tlaKeyDownHandler(event:KeyboardEvent):void
            {
                trace("Top Level Application Key Down Handler: key = " + event.keyCode);
            }

            protected function smKeyDownHandler(event:KeyboardEvent):void
            {
                trace("System Manager Key Down Handler: key = " + event.keyCode);
            }




        ]]>
    </fx:Script>

    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <s:Button x="130" y="135" label="Button"/>
    <s:ComboBox x="130" y="54"/>
    <s:TextInput x="130" y="99"/>
</s:Application>

这是一个输出:

Key Down Handler: key = 65

Top Level Application Key Down Handler: key = 65

System Manager Key Down Handler: key = 65

Key Down Handler: key = 83

Top Level Application Key Down Handler: key = 83

System Manager Key Down Handler: key = 83

Key Down Handler: key = 68

顶级应用程序 Key Down Handler: key = 68

System Manager Key Down Handler: key = 68

如您所见,我在应用程序中创建了三个不同的组件并使用三种不同的方法来捕获事件。在所有情况下(当其中一个组件具有焦点时)事件都会被触发并捕获。

也许你没有告诉我们一些重要的事情。

Hmm... I don't really understand what's the problem.

Here's a simple app:

    <?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"
               keyDown="application1_keyDownHandler(event)"
               creationComplete="application1_creationCompleteHandler(event)"
               >

    <fx:Script>
        <![CDATA[
            import flash.utils.getQualifiedClassName;

            import mx.core.FlexGlobals;
            import mx.events.FlexEvent;
            protected function application1_keyDownHandler(event:KeyboardEvent):void
            {
                trace("Key Down Handler: key = " + event.keyCode);
            }

            protected function application1_creationCompleteHandler(event:FlexEvent):void
            {
                FlexGlobals.topLevelApplication.addEventListener(KeyboardEvent.KEY_DOWN, tlaKeyDownHandler);
                FlexGlobals.topLevelApplication.systemManager.addEventListener(KeyboardEvent.KEY_DOWN, smKeyDownHandler);
            }

            protected function tlaKeyDownHandler(event:KeyboardEvent):void
            {
                trace("Top Level Application Key Down Handler: key = " + event.keyCode);
            }

            protected function smKeyDownHandler(event:KeyboardEvent):void
            {
                trace("System Manager Key Down Handler: key = " + event.keyCode);
            }




        ]]>
    </fx:Script>

    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <s:Button x="130" y="135" label="Button"/>
    <s:ComboBox x="130" y="54"/>
    <s:TextInput x="130" y="99"/>
</s:Application>

Here's an output:

Key Down Handler: key = 65

Top Level Application Key Down Handler: key = 65

System Manager Key Down Handler: key = 65

Key Down Handler: key = 83

Top Level Application Key Down Handler: key = 83

System Manager Key Down Handler: key = 83

Key Down Handler: key = 68

Top Level Application Key Down Handler: key = 68

System Manager Key Down Handler: key = 68

As you can see, I created three different components in the application and used three different methods to catch the event. And in all cases(when one of the components have focus) event is fired and catched.

Probably you didn't tell us something important.

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