Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capturing KeyPress Events in Android WebView

Tags:

android

Currently, my application have a webView that loads certain forms/websites etc.

Now I want to capture the KeyPressEvents within this webView. Below is the code I am using:

@Override
    public boolean dispatchKeyEvent(KeyEvent KEvent) 
    {
        int keyaction = KEvent.getAction();

        if(keyaction == KeyEvent.ACTION_DOWN)
        {
            int keycode = KEvent.getKeyCode();
            int keyunicode = KEvent.getUnicodeChar(KEvent.getMetaState() );
            char character = (char) keyunicode;

            Toast.makeText(this,"DEBUG MESSAGE KEY=" + character + " KEYCODE=" +  keycode, Toast.LENGTH_SHORT).show();
        }


        return super.dispatchKeyEvent(KEvent);
    }

Using the above code, I could only capture for backspace and numbers. Alphabets and special characters are not reflected.

Is it suppose to be like that? Or I must create my own app specific keyboard to capture the keypress events (android app specific soft keyboard)?

like image 376
aandroidtest Avatar asked Apr 11 '26 02:04

aandroidtest


1 Answers

Just put this code in your webview and everything will work as a charm.

public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
   return new BaseInputConnection(this, false); 
}
like image 88
mohan mishra Avatar answered Apr 12 '26 15:04

mohan mishra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!