向 flex 应用程序添加键盘快捷键 [英] Adding key board shortcuts to a flex application

查看:40
本文介绍了向 flex 应用程序添加键盘快捷键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 flex 应用程序,其中包含许多自定义组件,例如自定义按钮、组合框等.我正在尝试向 flex 应用程序添加键盘快捷键.在这样做时,我向应用程序添加了一个按键监听器来监听 ctrl+shift 组合键,如下所示:

I have a flex app with lots of custom components, like custom buttons, comboBoxes etc. I am trying to add keyBoard shortcuts to the flex app. In doing so, I added a key-down listener to the app to listen for ctrl+shift key combination like this:

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

然后我发送一个自定义事件,我的所有自定义组件都在侦听该事件:

then I dispatch a custom event that all of my custom components are listening for:

private function reportKeyDown(event:KeyboardEvent):void
{
    var evtObj:Object = new Object();
    evtObj.keyEvent = event;
    dispatchEvent(new CustomEvent(CustomEvent.SHORTCUT_KEYS_PRESSED, evtObj, true, false));
}

在我的自定义按钮组件中,我有:

In my custom button component I have:

this.addEventListener(CustomEvent.SHORTCUT_KEYS_PRESSED, ShortCutKeysHandler, true);

所以,如果我使用 ctrl+shift+W 那么我想要自定义按钮的一个实例被点击.

So, if I go ctrl+shift+W then I want one instance of the custom button to get clicked.

由于某种原因,该事件永远不会被触发,也永远不会到达 ShortCutKeysHandler 函数.

For some reason, the event never gets triggered and never gets to the ShortCutKeysHandler function.

有人知道怎么做吗?

推荐答案

我的猜测是您的事件没有冒泡给听众.如果您严格寻找键盘事件,并且不需要任何额外的有效负载,那么在您感兴趣的组件中简单地侦听舞台事件可能会更有效,或者让舞台调度您的自定义事件并使用 stage在您感兴趣的组件中使用 .addEventListener 而不是 this.addEventListener.

My guess is that your event isn't bubbling to the listeners. If you are strictly looking for keyboard events, and don't need any additional payload, perhaps it would be more effective to simply listen for the stage events in your interested components, alternatively have the stage dispatch your custom event and use stage.addEventListener in your interested components instead of this.addEventListener.

作为惯例,我从不依赖事件冒泡.它会混淆您的应用程序并造成歧义.最好明确定义您的事件结构,以获得对应用程序的额外控制并使调试更容易.

As a convention, I never rely on event bubbling. It obfuscates your application and creates ambiguity. It is better to explicitly define your event structure to gain additional control of your application and make debugging much easier.

这篇关于向 flex 应用程序添加键盘快捷键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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