Trigger a KeyboardEvent?


How can I manually trigger a keyboard event so that all objects that have a KeyboardEvent listener will receive it?
I need to have a button that simulate a keyboard key press (to build a virtual keyboard) but I don’t know how to tell my button to trigger this KeyboardEvent once I have built it…


Replying off the top of my head, but I believe you can dispatch a new KeyboardEvent to the stage, and it would get picked up by any listeners.


You should set it to bubble = true! Then I think it’ll propagate through the display list


Works perfectly. Thanks!

I was trying to do:

private class VkeyDispatcher extends EventDispatcher
  public function new(){
  public function sendKeyPressed(value:Int):Void{
	 var kbe:KeyboardEvent = new KeyboardEvent(KeyboardEvent.KEY_DOWN, true, true, value, value);


new VkeyDispatcher().sendKeyPressed(Keyboard.value);
on button click but it didn’t work (don’t really know why) as none of my DisplayObject with a keyboard listener was reciving the event (Even though it was supposed to be dispatched as the “Dispatched” trace message was correctly returned).

But by dispatching the event with state.dispatchEvent(), as you advised, work just fine.
Thanks again.


Great that it worked! :smile:


adding to trigger keyboardEvent… what if i am assigning data to a text field which listens to a keyboardevent. (XSS for test case)

let suppose i have a text field which listen to every alphabets key and digits key and i want to fill that field with javascript (by running the script in chrome console)


I think you would have to @:expose a function to the global JavaScript scope, so that you can call it later easily from a console