[英]How do you add pageup/pagedown keybindings to TextArea in python-prompt-toolkit?
Let's use calculator.py for example.让我们以calculator.py为例。
To add a scrollbar that works with the mouse wheel, you would change:要添加与鼠标滚轮配合使用的滚动条,您需要更改:
output_field = TextArea(style="class:output-field", text=help_text)
to:至:
output_field = TextArea(style="class:output-field", text=help_text, scrollbar=True)
But what would you add or change to scroll the TextArea with the page up and page down keys?但是,您会添加或更改什么以使用向上翻页和向下翻页键滚动 TextArea?
# The key bindings.
kb = KeyBindings()
@kb.add("pageup")
def _(event):
# What goes here?
pass
@kb.add("pagedown")
def _(event):
# What goes here?
pass
The simplest way would probably be to import focus_next
(or focus_previous
)最简单的方法可能是导入
focus_next
(或focus_previous
)
from prompt_toolkit.key_binding.bindings.focus import focus_next
and bind it to Control-Space (or anything else).并将其绑定到 Control-Space(或其他任何东西)。
# The key bindings.
kb = KeyBindings()
kb.add("c-space")(focus_next)
You could also, to seemingly keep the focus on input_field
, import scroll_page_up
and scroll_page_down
您也可以,为了看起来将注意力集中在
input_field
上,导入scroll_page_up
和scroll_page_down
from prompt_toolkit.key_binding.bindings.page_navigation import scroll_page_up, scroll_page_down
then switch focus to output_field
, call scroll_page_up
/ scroll_page_down
and finally change focus back to input_field
.然后将焦点切换到
output_field
,调用scroll_page_up
/ scroll_page_down
最后将焦点切换回input_field
。
# The key bindings.
kb = KeyBindings()
@kb.add("pageup")
def _(event):
w = event.app.layout.current_window
event.app.layout.focus(output_field.window)
scroll_page_up(event)
event.app.layout.focus(w)
@kb.add("pagedown")
def _(event):
w = event.app.layout.current_window
event.app.layout.focus(output_field.window)
scroll_page_down(event)
event.app.layout.focus(w)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.