如何确定在旧版 Android sdk (<12) 中按下了哪个键
我遇到了一个问题,我编写了一个覆盖 onKeyDown 的函数,所以为了知道按下的按钮,我写了这样的:
KeyEvent.keyCodeToString(keyCode).replace("KEYCODE_", "");
一切正常,但是当我尝试在带有 sdk < 的旧设备中运行该应用程序时12(3.1)它说没有这样的方法。
那么我怎样才能知道按下的按钮值呢?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,该方法是后来添加的,尽管作为帮助,您可以使用
getDisplayLabel
作为基本键,但这只会返回一个代表所按下按钮标签的字符。现在,由于 KeyEvent 是整数而不是枚举,因此获取代码的名称并不容易,因为它只是变量的名称。您可以尝试使用反射来获取键码的变量名称,但如果您开始记录用户按下的每个按钮,这似乎是一个沉重的机制......
Well, the method was added later though as a help you can use
getDisplayLabel
for basic keys but this will only return you one char representing the label of the button pressed.Now, as the KeyEvents are Ints and not an enum, it's not really easy to get the name of the code as it's only the name of the variable. You could try using reflection to get the keycode's variable name but it seems a heavy mechanism if you start logging every button the user presses...