1

Let's use calculator.py for example.

To add a scrollbar that works with the mouse wheel, you would change:

output_field = TextArea(style="class:output-field", text=help_text)

calculator.py without scrollbar

to:

output_field = TextArea(style="class:output-field", text=help_text, scrollbar=True)

calculator.py with scrollbar

But what would you add or change to scroll the TextArea with the page up and page down keys?

# The key bindings.
kb = KeyBindings()

@kb.add("pageup")
def _(event):
    # What goes here?
    pass

@kb.add("pagedown")
def _(event):
    # What goes here?
    pass

1 Answer 1

2

Change focus

The simplest way would probably be to import focus_next (or focus_previous)

from prompt_toolkit.key_binding.bindings.focus import focus_next

and bind it to Control-Space (or anything else).

# The key bindings.
kb = KeyBindings()

kb.add("c-space")(focus_next)

Keep focus

You could also, to seemingly keep the focus on input_field, import scroll_page_up and 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.

# 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)
1
  • The second one worked perfectly for my use case, thank you! In my case, I need to always keep focus on the input field while having the option to scroll the output field. The input field acts as the command prompt and the output field acts as the command output/history.
    – Tyler
    Mar 23, 2021 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.