PDA

View Full Version : How to get keyCode from input TextField?



maxell
August 25th, 2009, 03:41 PM
I want to detect which keys are pressed while user input text into TextField.
I tried something like

import flash.text.TextFormat;
import flash.text.TextField;
import flash.events.Event;
import flash.events.KeyboardEvent;

txt:TextField = new TextField;
txt.type = TextFieldType.INPUT;
txt.background = false;
txt.border = false;
txt.multiline = false;
txt.width = 226;
txt.height = 36;
txt.maxChars = 17;

var txt_format:TextFormat = new TextFormat ();
txt_format.font="Impact";
txt_format.size=36;
txt_format.color=0xFFB000;
txt_format.align="left"
txt_format.leftMargin=15;

txt.setTextFormat (txt_format);
addChild(txt);

txt.addEventListener (KeyboardEvent.KEY_DOWN, keyDownHandler);

function keyDownHandler (event:KeyboardEvent):void
{

trace (event.keyCode);}

but this doesn't work.

I am quite beginner in as3, so if anyone can show me where I made mistake, and how to make it works? Thanks

stolex
August 25th, 2009, 04:29 PM
Keyboard events work fine if you are focused to that text field. But what means next line in your code:

addChild(ime);

?

Your text field (txt) are not on stage. Try addChild(txt), and than click on that text field (mouse cursor will be changed when you are over), and when you type some character you will see trace

maxell
August 25th, 2009, 04:59 PM
I mistakenly wrote addChild (ime).