composerMike
composerMike

Reputation: 1096

flash - getting keyboard focus without clicking

In an Actionscript 3 program I used this code to add an event listener to a TextField:

var tf:TextField = new TextField();
tf.addEventListener(KeyboardEvent.KEY_DOWN, handleText);

handleText() only gets called if I click on the TextField. Is there a way to receive keyboard events when the mouse is just hovering over the TextField (no need to click)?

Upvotes: 0

Views: 196

Answers (1)

Vesper
Vesper

Reputation: 18747

A TextField has its own keyboard event listener that it uses to alter text, if its mode is input. And to capture keyboard events while your target does not have focus, add the listener to stage, and check if your mouse is over the text field in question. For this, you can set a boolean flag in MOUSE_OVER and drop it in MOUSE_OUT listener attached to the textfield. Then, if the flag is on, you can either parse the keyboard event yourself or transfer it to the TextField via dispatchEvent() call with existing KeyboardEvent object.

Upvotes: 1

Related Questions