How to simulate a PressKey operation in ActionScript3

Hi, I'd like an example about simulation a "press key" operation in AS3 (AIR or Flex)
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

are you asking about how to dispatch key events?  that's possible, but you can't completely recreate a key press (like you can in VB).  basically, you can force an element with a listener to react as if a key had been pressed, but you can't recreate default behavior (you couldn't use fake key events to write in an input text field, for example).

the example above shows what you can do - as you can see, nothing is actually written to the textfield, and the keyCode returned is always 0 when dispatched programmatically

// create the textfield
var tf:TextField = new TextField();
tf.text = "some text";
tf.type = "input";

// define the handler
function keydownHandler(event:KeyboardEvent):void{
// set up the listener
tf.addEventListener(KeyboardEvent.KEY_DOWN, keydownHandler);

// "fake" a key press
tf.dispatchEvent(new KeyboardEvent(KeyboardEvent.KEY_DOWN));

Open in new window

Actually, it is possible to have textFields update as if a real key was pressed if you press down on the keyboard, but it requires a little tinkering.

A while back I helped someone get an onscreen keyboard working, which updates textFields based on which keys were "clicked". The post almost at the end is the complete one.

Do you need help adapting that code to your situation?
it's not possible to emulate a true key press, was my point.  of course you can add listeners and catch keycodes, then dynamically add text, but you can't simulate an actual key event - no web based technology allows this for obvious reasons (it would be a massive security hole).

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Adobe Flash

From novice to tech pro — start learning today.