如何确定在旧版 Android sdk (<12) 中按下了哪个键

发布于 2025-01-04 01:55:53 字数 232 浏览 4 评论 0 原文

我遇到了一个问题,我编写了一个覆盖 onKeyDown 的函数,所以为了知道按下的按钮,我写了这样的:

KeyEvent.keyCodeToString(keyCode).replace("KEYCODE_", "");

一切正常,但是当我尝试在带有 sdk < 的旧设备中运行该应用程序时12(3.1)它说没有这样的方法。

那么我怎样才能知道按下的按钮值呢?

I've got a problem, I wrote a function that override the onKeyDown, so to know the button pressed i wrote this:

KeyEvent.keyCodeToString(keyCode).replace("KEYCODE_", "");

All worked but when i tried to run the app in a older device with an sdk < 12 (3.1) it says that there are no such method.

So how can i do to know the pressed button value?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小嗲 2025-01-11 01:55:53

好吧,该方法是后来添加的,尽管作为帮助,您可以使用 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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文