1、当安卓设备插入USB键盘和USB扫码枪
2、需求dialog要按键盘回车键生效,扫码枪的回车不能生效
3、输入设备又不能固定品牌,只能通过此方法
//区分扫码枪Enter事件和普通按键Enter事件
private var mLastEnterKeyDownTime = 0L
private val mOnKeyListener = DialogInterface.OnKeyListener { _, keyCode, event ->
LogUtils.e("$mTag -> $event")
val now = System.currentTimeMillis()
when (event.action) {
KeyEvent.ACTION_DOWN -> {
when (keyCode) {
KeyEvent.KEYCODE_ENTER -> mLastEnterKeyDownTime = now
}
false
}
else -> when (keyCode) {
KeyEvent.KEYCODE_ENTER -> {
//关键判断 扫码枪 down到up的时间特别短
val isScannerEnterKey = now - mLastEnterKeyDownTime < 10
isScannerEnterKey
}
else -> false
}
}
}