WebView onKeyListener not working

I have to intercept the touch on the screen, and this is my code:

    mWebView = findViewById(;
    mWebView.setWebViewClient(new WebViewClient());

and my listeners:

public boolean onTouch(View view, MotionEvent motionEvent) {
    if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
        Log.i(TAG, "Hello, touch.");
        return false;
    return false;

public boolean onKey(View view, int i, KeyEvent keyEvent)
    Log.i(TAG, "Hello, onKey.");
    return false;

While the on touch is working fine for touches on the screen, it dosen’t intercept the touch event on the keyboard. For this reason, I’ve added the onkeylistener, but it’s not intercepting the key events. As last thing, I don’t care about what is typed, but just if the screen has been touched.

Stack Overflow Asked by Federico Ponzi on November 15, 2021

3 Answers

3 Answers

why not trying onkeyDown?

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(event.getAction() == KeyEvent.ACTION_DOWN){
        case KeyEvent.KEYCODE_BACK:
            return true;

    return super.onKeyDown(keyCode, event);


override this method --> onUnhandledKeyEvent (WebView view, KeyEvent event)

more details on:,%20android.view.KeyEvent)


public boolean dispatchKeyEvent(KeyEvent event) {
    boolean dispatchFirst = super.dispatchKeyEvent(event);
    // Listening here for whatever key events you need
    if (event.getAction() == KeyEvent.ACTION_UP)
        switch (event.getKeyCode()) {
            case KeyEvent.KEYCODE_SPACE:
            case KeyEvent.KEYCODE_ENTER:
                // e.g. get space and enter events here
    return dispatchFirst;


Preventing developers from accessing the events by default was made by Googlers on purpose. Because the key event input isn't the only one anymore. There're gestures, voice and more is coming. Official recommendation is to "stop relying on legacy key events for text entry at all". Check out more details here:

Answered by Wini on November 15, 2021

If you want to detect keypress then try to override

onKeyPreIme(int keyCode, KeyEvent event);

Answered by Saikrishna Rajaraman on November 15, 2021

With my recent experience of inexplicable and inconsistent behaviour of calling loadUrl() before setWebViewClient() I would try calling loadUrl() AFTER setting ANYTHING to do with a multi-threaded WebView. That includes setOnTouchListener() and setOnKeyListener(). I realise my answer might not help your specific problem - but it might help others who have experienced similar behaviour and are setting things before before calling loadUrl().

Answered by Frost Metoh on November 15, 2021

Add your own answers!

Related Questions

react ref and query selector all

2  Asked on February 10, 2021 by peter-flanagan


Direct code execution through the JS browser console

0  Asked on February 10, 2021 by matheus-nascimento


How to correctly write word-break in css?

3  Asked on February 10, 2021 by aiko-schurmann


Finding a user data based on email

1  Asked on February 10, 2021 by aamer-salame


Can I use text when using functions? Or only numbers?

4  Asked on February 9, 2021 by osk6r


Get the string before replace

0  Asked on February 9, 2021 by tanker


Why NCat overs immediately if I call it by Popen?

1  Asked on February 9, 2021 by shkiperdesna


Understanding Generic Class implementation

3  Asked on February 9, 2021 by yeo


full join of the several data frames of nested data frame

2  Asked on February 9, 2021 by seyed-hosseini


Ask a Question

Get help from others!

© 2021 All rights reserved.