如何收听带有输入连接的视图发送到视图的软密钥事件?

发布于 2025-01-20 03:25:30 字数 936 浏览 3 评论 0原文

我想在Android中创建自定义的自定义文本编辑器视图,并具有可自定义的键控件,这意味着我需要处理所有软件键盘密钥事件。

为此,我可以扩展view并覆盖其onkeydown事件。但是,我还想保留用户的完整软件键盘功能,例如建议和滑行型,这似乎只有在我正确地覆盖increateinputConnection之类的情况下才能有效:

override fun onCreateInputConnection(outAttrs: EditorInfo): InputConnection {
    outAttrs.inputType = InputType.TYPE_CLASS_TEXT
    return object : BaseInputConnection(this, true) { ... }
}

不幸的是,不幸的是,inputconnection inputConnection < /code>似乎会吃掉所有关键事件,并阻止诸如onkeydown之类的关键侦听器方法,而我想收听关键事件并使用inputconnection(同时允许完整的软键盘功能)。

我发现的唯一解决方法是使用textwatcher收听文本更改事件,但我认为这不像我需要那样灵活或简单。我需要完全直接访问所有钥匙按压。

我相信这是可能的,因为这正是input element WebView 工作的方式:我可以收听其关键事件,用户可以输入带有建议或Glide的文本 - 同时打字。实际上,毕竟我正在考虑使用WebView,但是我不明白为什么如果没有一个,就不可能这样做。

如何在Android中实现此行为,无论是在视图还是可组合的视图中?

I would like to create a custom text editor view in Android, with customizable key controls, meaning that I need to handle all software keyboard key events.

To do this, I can extend View and override its onKeyDown event. However, I would also like to preserve the user's full software keyboard functionality, e.g. suggestions and glide-typing, which seems to only work if I properly override onCreateInputConnection like so:

override fun onCreateInputConnection(outAttrs: EditorInfo): InputConnection {
    outAttrs.inputType = InputType.TYPE_CLASS_TEXT
    return object : BaseInputConnection(this, true) { ... }
}

Unfortunately, the InputConnection seems to eat all the key events and block the key listener methods like onKeyDown from being called, whereas I would like to listen to key events and use an InputConnection (to allow full soft keyboard functionality) at the same time.

The only workaround I have found is using a TextWatcher to listen to text change events, but I don't feel this is as flexible or simple as I need it to be; I need full and direct access to all key presses.

I believe this is possible because this is exactly how an input element in a WebView works: I can listen to its key events and the user can input text with suggestions or glide-typing at the same time. In fact, I am considering using a WebView after all, but I don't see why it shouldn't be possible to do this without one.

How can I implement this behavior in Android, in either a View or a Composable?

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

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

发布评论

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

评论(1

戒ㄋ 2025-01-27 03:25:30

使用软键盘时不存在 KeyEvent。几乎每个软键盘都使用 InputConnection.commitText() 一次发送字符甚至单词。 KeyEvents 仅由硬件键盘、蓝牙键盘和音量等物理键真正生成。

如果您想查找文本更改,请实现 commitText() 函数并查看发送的内容。您还需要重写一两个其他函数(例如deleteSurroundingText)才能按下删除键。

KeyEvents don't exist when using a soft keyboard. Almost every soft keyboard uses InputConnection.commitText() to send characters or even words at a time. KeyEvents are only really generated by hardware keyboards, bluetooth keyboards, and physical keys like volume.

If you want to look for changes to text, implement the commitText() function and see what's sent over there. There's one or two other functions like deleteSurroundingText you'd need to override as well to get delete presses.

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