我目前正在尝试实现一个框架,以捕获每个用户与应用程序的交互。我有一个可以捕获用户按键事件的活动,如下:
@Override
public boolean dispatchKeyEvent(@NonNull KeyEvent kEvent) {
final int action = kEvent.getAction();
final int keyCode = kEvent.getKeyCode();
final char character = (char) kEvent.getUnicodeChar();
if (action == KeyEvent.ACTION_UP) {
Log.i(">>>>DEBUG", "KEY EVENT");
final long res = kEvent.getEventTime() - this.timestampLastKeyboardEvent;
switch (keyCode) {
case KeyEvent.KEYCODE_DEL:
// FIXME
this.capuccinoEventLogger.removeLastCharacter();
break;
case KeyEvent.KEYCODE_TAB:
case KeyEvent.KEYCODE_ENTER:
this.capuccinoEventLogger.markLastCapuccinoEventFinal();
break;
default:
// HACK: there is a strange behaviour when a special character is
// typed the current method is invoked twice, one for the normal
// key and the other for the shift + key code.
if (res != 0) {
CapuccinoKeyboardEvent ce = new CapuccinoKeyboardEvent(character);
this.capuccinoEventLogger.addNewCapuccinoEvent(ce);
}
break;
}
this.timestampLastKeyboardEvent = kEvent.getEventTime();
}
return super.dispatchKeyEvent(kEvent);
}
问题是,当我尝试从EditText
捕获事件时,除了退格键,每个键都被检测到。为什么会这样?
答案 0 :(得分:0)
很多人有同样的问题。我找到了一个很好的链接: android-cannot-capture-backspace-delete-press-in-soft-keyboard
但是,如果您想要一个简单的解决方案,则可以执行以下操作:
@canerkaseler