问题描述
我正在使用 Kivy 框架编写一个应用程序,我偶然发现了一个小而烦人的问题:我不知道如何处理文本字段中的 Tab/Enter/Arrow 键,以便按下任一其中会派发一个事件,例如.将焦点(跳转)到另一个 TextInput 或启动类似 send_form()
I'm writing an app using Kivy framework and I stumbled upon a minor but annoying problem: I don't know how to handle Tab/Enter/Arrow keys in text fields so that pressing either of them would dispatch an event, eg. switch the focus (jump) to another TextInput or launch something like send_form()
谁能解释一下这个问题?
Could anyone please shed some light on this issue?
推荐答案
正如 Daniel Kinsman 在他的评论中所建议的,您可以继承 TextInput,添加上一个"和下一个"ObjectProperties 以支持选项卡(易于在 kv 中使用引用其他小部件),并以不同的方式处理键盘事件.目前没有开箱即用的支持,但如果您想进行此类修改,请向我们发送功能请求或在 freenode 上的#kivy 中讨论它.
As suggested by Daniel Kinsman in his comment, you could subclass TextInput, add "previous" and "next" ObjectProperties for tab support (easy to set in kv using references to other widgets), and handle the keyboard events differently. There is no out of the box support for this right now, but if you want to work on such modification drop us a feature-request or comme discuss it in #kivy on freenode.
https://github.com/kivy/kivy/blob/master/kivy/uix/textinput.py#L1188
也许在小部件上添加这样的支持会更好,并添加一些焦点逻辑,因此 tab/enter 对任何可激活的小部件都有影响,并且像滑块这样的一些小部件也使用右/左/上/下键.
Maybe it would be even better to add such support on widget, and add some focus logic, so tab/enter have effects on any activable widget, and some widgets like slider use right/left/up/down keys too.
因此,在 Kivy 中还有很多工作要做,如果您有兴趣提供帮助,您真的可以更快地实现,我们会帮助您:)
So there is still a lot to do in Kivy about that, and if you are interested in helping, you can really make it happen faster, we'll help you :)
这篇关于Kivy 的 TextInput 小部件中的 Tab/Enter(和其他击键)处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!