3

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)?

Nimantha
  • 6,405
  • 6
  • 28
  • 69
aandroidtest
  • 1,493
  • 8
  • 41
  • 68

2 Answers2

3

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

public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
   return new BaseInputConnection(this, false); 
}
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
mohan mishra
  • 1,158
  • 2
  • 14
  • 21
0

OnKeyListener can be used for capturing key events on webView.

    mWebView.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // You can get any keyCode or KeyEvent here
            return false;
        }
    });

http://developer.android.com/reference/android/view/View.OnKeyListener.html

Devrim
  • 15,345
  • 4
  • 66
  • 74