简体   繁体   中英

Detect Keyboard event in Android 4.0+ WebView using JavaScript

I'd like to detect keyboard click event (soft keyboard or physical keyboard)

I have a simple html and JS that runs fine in various browser in Mac/Windows, even in Android's Chrome browser (both soft and physical keyboard can be detected). I understand that WebView may not be based on Chrome (until 4.4)

document.onkeyup = function(event){
    alert("found keyup with value: " + event.which); 
};

In an Android app, I have a WebView that open the same webpage,

Somehow the above code can't detect any key press. I have tried using different ways to get the key event, such as JQuery:

$(document).keyup(function(event){
    alert("keyup with value: " + event.which); 
 });    

Is keyboard event disabled in WebView in Android?

Solution is to request focus...

    _webView = (WebView) findViewById(R.id.my_super_webview);
    _webView.requestFocus(View.FOCUS_DOWN);

and not all keyboard press can be detected, for example, the Android 'menu' button

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM