通过 Android API 发送组合键(元键和键码)
我不知道如何使用 Android 发送元键(例如 CTRL)和键码(例如 RETURN)的组合(我使用 API 级别 11 = 版本 3.0)。
类 KeyEvent 的文档提到了常量与META_CTRL_ON类似,并且还支持元键的键码常量(例如KEYCODE_CTRL_LEFT)。
我正在使用 Javascript 键事件测试器 来测试我的输入法编辑器生成的输出(输入法)。顺便说一句,我的目标是开发一个软件键盘。
如果我对文档的理解正确,那么执行以下代码就足以仅发送 CTRL 键:
this.sendDownUpKeyEvents(KeyEvent.KEYCODE_CTRL_RIGHT);
但是当针对 Javascript 键事件测试器执行此操作时(见上文),什么也没有发生。
因此,我需要了解如何仅发送元键和如何发送元键与另一个键的组合。我还尝试了以下发送SHIFT+ENTER(具体示例):
private void _sendShiftEnter() {
this.sendDownKeyEvent(KeyEvent.KEYCODE_SHIFT_LEFT);
final long eventTime = SystemClock.uptimeMillis();
this.getCurrentInputConnection().sendKeyEvent(
new KeyEvent(
eventTime, // The time (in uptimeMillis()) at which this key code originally went down.
eventTime, // The time (in uptimeMillis()) at which this event happened.
KeyEvent.ACTION_DOWN, // Action code: either ACTION_DOWN, ACTION_UP, or ACTION_MULTIPLE.
KeyEvent.KEYCODE_ENTER, // The key code.
0, // A repeat count for down events (> 0 if this is after the initial down) or event count for multiple events.
KeyEvent.META_SHIFT_ON, // Flags indicating which meta keys are currently pressed.
0, // The device ID that generated the key event.
0, // Raw device scan code of the event.
KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE, // The flags for this key event.
InputDevice.SOURCE_KEYBOARD // The input source such as SOURCE_KEYBOARD.
)
);
}
这里也出现了与上面相同的问题:唯一可识别的键是ENTER。
我已经在互联网上搜索了几个小时的示例,了解如何将 KeyEvent 类与元键和/或组合键一起使用,但至少找不到一个代码示例。
所以,总而言之:有没有人有过使用 KeyEvent 类的经验,并且可以向我演示如何通过 Android API 发送简单的按键组合(例如 SHIFT+ENTER)?
先感谢您!
I can't figure out how to send a combination of a meta key (e.g. CTRL) and a keycode (e.g. for RETURN) with Android (I am using API level 11 = version 3.0).
The documentation of the class KeyEvent mentions constants like META_CTRL_ON and also supports keycode constants (e.g. KEYCODE_CTRL_LEFT) for meta keys.
I am using the Javascript Key Event Tester to test the output that is generated by my Input Method Editor (IME). BTW, my goal is to develop a software keyboard.
If I understand the documentation correct, it would be sufficient to execute the following code to send the CTRL key only:
this.sendDownUpKeyEvents(KeyEvent.KEYCODE_CTRL_RIGHT);
But when this is executed against the Javascript Key Event Tester (see above), nothing happens.
So I need to get a clue how to send meta keys only and to send meta keys in combination with another key. I also tried the following to send SHIFT+ENTER (a concrete example):
private void _sendShiftEnter() {
this.sendDownKeyEvent(KeyEvent.KEYCODE_SHIFT_LEFT);
final long eventTime = SystemClock.uptimeMillis();
this.getCurrentInputConnection().sendKeyEvent(
new KeyEvent(
eventTime, // The time (in uptimeMillis()) at which this key code originally went down.
eventTime, // The time (in uptimeMillis()) at which this event happened.
KeyEvent.ACTION_DOWN, // Action code: either ACTION_DOWN, ACTION_UP, or ACTION_MULTIPLE.
KeyEvent.KEYCODE_ENTER, // The key code.
0, // A repeat count for down events (> 0 if this is after the initial down) or event count for multiple events.
KeyEvent.META_SHIFT_ON, // Flags indicating which meta keys are currently pressed.
0, // The device ID that generated the key event.
0, // Raw device scan code of the event.
KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE, // The flags for this key event.
InputDevice.SOURCE_KEYBOARD // The input source such as SOURCE_KEYBOARD.
)
);
}
The same problem as above occurs here, too: The only recognized key is ENTER.
I already searched the Internet for several hours for examples, how to use the KeyEvent class with meta keys and/or key combinations, but couldn't find at least one example of code.
So, in conclusion: Has anyone experience with the KeyEvent class and can demonstrate me how to send a simple combination of keys (e.g. SHIFT+ENTER) via the Android API?
Thank you in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我只是同时放置了两个元键修饰符,并且它起作用了......
例如
KeyEvent.META_SHIFT_LEFT_ON | KeyEvent.META_SHIFT_ON
。I just put both meta key modifiers at the same time, and it worked...
for example
KeyEvent.META_SHIFT_LEFT_ON | KeyEvent.META_SHIFT_ON
.