Krishna Chauhan
Krishna Chauhan

Reputation: 141

onKeyDown, dispatchKeyEvent etc on android WebView for Key tracking

I am developing an app that track the key while entering the data in WebView.. Is it possible to have a onKeyEvent, dispatchKeyEvent on WebView... I've already searched a lot on the internet but couldn't find the perfect solution for it... tried almost everything...

Upvotes: 0

Views: 1962

Answers (2)

DemoDemo
DemoDemo

Reputation: 392

The question is a bit old, this solution that I found is not perfect but you actually can catch keyboard key presses as long you have only one type of fields (meaning text/number, etc...), but maybe it will be helpful to someone.

public class WebViewGo extends WebView {
    public WebViewGo(Context context, AttributeSet attrs) {
        super(context, attrs);
    }



    @Override
    public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
        BaseInputConnection inputConnection = new BaseInputConnection(this, false);
        outAttrs.imeOptions = EditorInfo.IME_ACTION_DONE;
       outAttrs.inputType = EditorInfo.TYPE_CLASS_NUMBER;
        return inputConnection;
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        boolean isDispached = super.dispatchKeyEvent(event);

        if(event.getAction() == KeyEvent.ACTION_UP){
            switch (event.getKeyCode())
            {
                case KeyEvent.KEYCODE_ENTER:
                    InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Activity.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(getWindowToken(), 0);
                break;
            }
        }

        return isDispached;
    }


}

I took the code from this link

Upvotes: 0

Florang
Florang

Reputation: 258

I had the same problem. It seems impossible to me to catch event from the keyboard on a webView (based on : https://code.google.com/p/android/issues/detail?id=66715).

I had to implement my own keyboard for my app. I now can easily catch event from my keyboard.

If you want to do it yourself too, I advice you to follow this tutorial : http://www.fampennings.nl/maarten/android/09keyboard/index.htm or this one if you want to use your keyboard outside your app : http://code.tutsplus.com/tutorials/create-a-custom-keyboard-on-android--cms-22615

Hope it helps and if you find another solution let us know please

Upvotes: 1

Related Questions