Android在没有edittext视图的情况下捕获硬件键盘事件
如何在不使用 EditText 字段的情况下捕获硬件键盘事件?
例如,在一个简单的活动中,在屏幕上显示一个正方形,当在滑动键盘上按下“B”时,我想将其变为蓝色,当按下“G”时,将其变为绿色,等等。
我不这样做不需要颜色代码的帮助,只是如何拦截按键
这与软键盘或虚拟键盘无关
How can I capture the hardware keyboard events without using an EditText field?
For example, in a simple activity the display a square on the screen, when a "B" is pressed on the slide keyboard I want to turn it blue, when a "G" is presses, turn it Green, etc.
I don't need help with the color code, just how to intercept the keypress
This is not about the soft or virtual keyboard
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Android 类通常提供事件处理程序,您可以在子类化它们时实现。
Activity
类具有以下事件处理程序:onKeyDown(int keyCode, KeyEvent event)
onKeyLongPress(int keyCode, KeyEvent event)
onKeyMultiple (int keyCode, int RepeatCount, KeyEvent event)
onKeyShortcut(int keyCode, KeyEvent event)
onKeyUp(int keyCode, KeyEvent event)
此外,所有视图都有以下事件处理程序:
onKeyDown(int, KeyEvent)
onKeyUp(int, KeyEvent)
我猜还有许多其他类具有类似的按键事件事件处理程序,但这应该是足够适合你的情况了。 KeyEvent 然后包含有关按下的按键的信息,即按键代码。
Android classes usually provide event handlers, you can implement when subclassing them. The
Activity
class has the following event handlers:onKeyDown(int keyCode, KeyEvent event)
onKeyLongPress(int keyCode, KeyEvent event)
onKeyMultiple(int keyCode, int repeatCount, KeyEvent event)
onKeyShortcut(int keyCode, KeyEvent event)
onKeyUp(int keyCode, KeyEvent event)
In addition all views have the following event handlers:
onKeyDown(int, KeyEvent)
onKeyUp(int, KeyEvent)
I guess there are many other classes that have similar event handlers for key events, but this should be enough for your situation. The KeyEvent then contains information about the pressed key, i.e. the key code.
Activity 类已经实现了 KeyEvent.CallBack 请参阅此处,
您只需覆盖这些方法并实现您想要的所有事件
Activity class has already implemented KeyEvent.CallBack see here
you just need to override these methods and implements all events that you want