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 R.id.right  and R.id.left  are buttons:

 

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

Screenshot_2015-01-28-18-20-44

 

Hope this will be usefull for you!

Leave a Reply