Send keyboards event to Android webview

posted in: Android, Guides, Java | 0

In the process of integrating a Javascript game into a webview I needed to have buttons injection keybard events in the webview.

The process is fairly simple and can be used on any type of view:

webView.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_LEFT));


You can choose any keypress event, here I used the left keyboard arrow.

Here is the complete snippet code:

and the layout where  and  are buttons:


Which looks like this with the HTML / JS and CSS loaded:



Hope this will be usefull for you!

Leave a Reply