3
votes

I'm creating a virtual keyboard for a touchscreen Flex app and i'm trying to simulate a key press by dispatching a KeyboardEvent. I've written a handler function to listen for the event and act accordingly. So far so good... but it's starting to get complicated as i have to manage the focused textInputs (easy), the cursor position in those fields (not to so easy), etc. Now, if only there was a way to actually dispatch a KeyboardEvent that Flex would actually interpret as a genuine key press all those issues would be gone... Is that possible?

1
I'm not sure I fully understand the control flow here. So the user will touch (i.e. click) on one of the buttons on the keyboard. This will generate a click event from which you generate a KeyboardEvent that will then tell the text box to add that character and reposition the cursor. Is this correct? And the spot where you're having the problem is when you generate the KeyboardEvent the text box doesn't update?Dan
Yes, that's correct. I can make that happen but by using a KeyboardEvent handler... if instead i just dispatch the event (with the TextInput focused) nothing happens.Tiago Pereira
Which component are you dispatching the KeyboardEvent on? I think to get things to work properly you'll need to dispatch the event on the text component with focus, i.e.: myFocusedTextComponent.dispatchEvent( myNewlyCreatedKeyboardEvent )Dan
My solution won't work, see Peter's answer below.Dan

1 Answers

4
votes

The TextInput does not use KeyboardEvent/TextEvent for text input, it uses internal Flash TextField objects that interact with the Flash Player / Keyboard.

The KeyboardEvent are used to enable notification of the Keyboard Event that occured.

To simulate a keyboard, you will need to create a class that upon recieving a KeyboardEvent will modify the text property of a TextInput and the cursor position accordingly.

Alex Harui has written a similar post about this FlexCoders Post