You can use the Instrumentation
class for this. I've tested it with Android 4.4 and it works.
First ensure that the WebView
has focus, then call sendCharacterSync()
to send individual key events. Note that these calls must be done from a background thread (this is mandatory).
For example:
final Instrumentation instrumentation = new Instrumentation();
new Thread(new Runnable()
{
@Override
public void run()
{
instrumentation.sendCharacterSync(KeyEvent.KEYCODE_H);
instrumentation.sendCharacterSync(KeyEvent.KEYCODE_I);
instrumentation.sendCharacterSync(KeyEvent.KEYCODE_TAB);
instrumentation.sendCharacterSync(KeyEvent.KEYCODE_T);
instrumentation.sendCharacterSync(KeyEvent.KEYCODE_H);
instrumentation.sendCharacterSync(KeyEvent.KEYCODE_E);
instrumentation.sendCharacterSync(KeyEvent.KEYCODE_R);
instrumentation.sendCharacterSync(KeyEvent.KEYCODE_E);
}
}).start();
The only issue is that you get a "typewriter" effect (i.e. the letters appear one by one, not all at once).