首页 > 代码库 > Android 输入法截取key优先于view
Android 输入法截取key优先于view
为了验证编写了个例子
首先是输入法
public class RemoteKeyboard extends InputMethodService { @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (processKey(event, 0 != event.getRepeatCount())) return true; return super.onKeyDown(keyCode, event); } @Override public boolean onKeyUp(int keyCode, KeyEvent event) { if (processKey(event, true)) return true; return super.onKeyUp(keyCode, event); } private boolean processKey(KeyEvent event, boolean doIt) { Log.e("======= " ," ============ processKey"); return false; } }
其次是app view
final TextView et1 = (TextView)findViewById(R.id.editText1); et1.setOnKeyListener(new OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { Log.e("====================","====OnKeyListener "); }
运行程序打印
============================ : ============ processKey
==================== :====OnKeyListener
============================ : ============ processKey
==================== :====OnKeyListener
第一遍应该是KeyDown, 第二遍应该是 KeyUp, 说明Android 输入法截取key优先于view
又试验了一下改写system/usr/keychars/Generic.kcm
把 B 改为
key B {
base: ‘b‘ fallback DPAD_CENTER
}
在一个不可编辑框, log 如下
12-23 05:18:09.093 118 118 E ============================ : ============ processKey
12-23 05:18:09.094 118 118 E ============================ : ============ processKey
12-23 05:18:09.163 118 118 E ============================ : ============ processKey
12-23 05:18:09.163 118 118 E ============================ : ============ processKey
说明b没有被 accept,而后又调用了 DPAD_CENTER
在一个可编辑框,b被accept, log 如下
============================ : ============ processKey
============================ : ============ processKey
说明没有调用 fallback
如果改法如下
key B {
base: fallback DPAD_CENTER
}
在一个可编辑框也要打4行log, 说明 fallback被调用了
Android 输入法截取key优先于view