Global eventListener, when focus inside the texiInput
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 :)
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);
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.
精彩评论