1、在phoneGap的android项目的主函数(继承 DroidGap那里)中添加键盘监听事件,android监听键盘必须在activity中
2、再注册一个js事件,
3、最后发送,在UI端使用document.addEventListener()实现监听
(备注,该好像方法必须有个输入框得到焦点才能触发)
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { String javaScriptEventTenplate="var e = document.createEvent(''Events'');\n"+ "e.initEvent(''{0}'');\n"+ "e.key = {1};\n"+ "document.dispatchEvent(e);"; String commad; if (keyCode == KeyEvent.KEYCODE_F9 || keyCode == KeyEvent.KEYCODE_F10 || keyCode == KeyEvent.KEYCODE_F11) { commad = MessageFormat.format(javaScriptEventTenplate,"PRESS_SCAN","'down'"); //注册一个js事件 appView.sendJavascript(commad); //发送事件 if(isScan == true){ barcodeControll.Barcode_StartScan(); new BarcodeReadThread().start(); } return true; } else if (keyCode == 82) { commad = MessageFormat.format(javaScriptEventTenplate,"press_menubutton","'down'"); appView.sendJavascript(commad); return true; }
else if (keyCode == 7) { this.loadUrl("javascript:cordova.fireDocumentEvent('press_0');"); return true; } return super.onKeyDown(keyCode, event); }
function down(){ 监听内容 }