admin管理员组文章数量:1426020
I have a simple WebView application which I want to control with the keyboard. Is it possible to catch arrow keys in Javascript?
I have tried the following code without any luck:
function handleArrowKeys(evt) {
console.info('key');
}
document.onkeyup = handleArrowKeys;
document.onkedown = handleArrowKeys;
document.onkepress = handleArrowKeys;
Javascript is enabled in the webview
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
I have a simple WebView application which I want to control with the keyboard. Is it possible to catch arrow keys in Javascript?
I have tried the following code without any luck:
function handleArrowKeys(evt) {
console.info('key');
}
document.onkeyup = handleArrowKeys;
document.onkedown = handleArrowKeys;
document.onkepress = handleArrowKeys;
Javascript is enabled in the webview
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
Share
Improve this question
edited Aug 14, 2019 at 8:26
Brian Tompsett - 汤莱恩
5,89372 gold badges61 silver badges133 bronze badges
asked Apr 16, 2012 at 10:59
barabara
3,0642 gold badges28 silver badges25 bronze badges
4
-
Have you tried
document.body.onkeydown
etc? – Lee Kowalkowski Commented Apr 16, 2012 at 12:08 - Yes and I do get key events from normal keys but no events from the arrow keys. The WebView does not trigger those. – bara Commented Apr 17, 2012 at 10:16
-
I have found
shouldOverrideKeyEvent
theWebViewClient
. I think this is going to the right direction – bara Commented Apr 17, 2012 at 10:58 - OK, cool. onkeypress will never trigger for arrow keys, incidentally. You have to use down/up for special keys. – Lee Kowalkowski Commented Apr 17, 2012 at 15:14
1 Answer
Reset to default 4You should overwrite the onKeyDown
method of WebView.
See: http://blog.csdn/focusxi/article/details/6780965
@Override
public boolean onKeyDown(int keyCode, KeyEvent event){
int valKey = 0;
System.out.println("Web KEY:");
System.out.println(keyCode);
switch(keyCode){
//UP
case 50:
case 19:
valKey = 19;
break;
//DOWN
case 83:
case 20:
valKey = 20;
break;
//LEFT
case 81:
case 21:
valKey = 21;
break;
//RIGHT
case 69:
case 22:
valKey = 22;
break;
}
if (valKey!=0)
{
//new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_SHIFT_LEFT);
KeyEvent event1 = new KeyEvent(KeyEvent.ACTION_DOWN, valKey);
System.out.println(event1.getKeyCode());
return super.onKeyDown(38, event1);
}
else
{
return super.onKeyDown(keyCode, event);
}
}
}
本文标签: Android WebView Handle arrow keys in JavaScriptStack Overflow
版权声明:本文标题:Android WebView: Handle arrow keys in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745459790a2659270.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论