yikulju

front-end development

keyDown in Flex 3

Oct 6th 2008
One Comment
respond
trackback

There is a annoying issue when you just start dealing with Flex 3 and want to use the keyboard.
It’s simple to say that you have to use the KeyboardEvent.KEY_DOWN but there are two things which you have to do right.
Firstly, let’s look at the following code that traces the key which was pressed.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="init()">
 <mx:Script>
	<![CDATA[
 
	public function init():void
	{
		trace('Initialization');
		stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);	
	}
 
	private function keyDown(event:KeyboardEvent):void
	{
		trace(event.charCode);
	}		
	]]>
 </mx:Script>
</mx:Application>
1. Using the applicationComplete
After all components are created and drawn, the Application object dispatches an applicationComplete event. This is the last event dispatched during an application startup.
In this case, the init() will be called after everything, therefore we have a stage instance.

2. stage.addEventListener()
We have to attach the listener to the stage to the this done.

This post is tagged , , ,

One Comment

  1. Thanks! You are 100% correct… this was HIGHLY annoying. I wasted enough time being close. You finished me off and saved me more aggravation. You have my gratitude!

Incoming Links