Capture keyboard events and suppress them #1509
-
It would be nice to capture keyboard events with the capability of handling such events to update a For example, say I want to make an interactive display and capture input if I do the following: def make_panel(text: str = "Default value..."):
text = Text(text)
panel = Panel(
text,
padding=(1, 2),
title=f"[b green]Enter a value to print, or 'Exit' to exit",
border_style="bright_green",
)
return panel
if __name__ == '__main__':
console = Console()
main = Layout("main") # this is effectively a view model
main.update(make_panel())
with Live(main, screen=False) as live:
while True:
string = console.input() # pressing keys here makes the screen jitter, need enter to capture input
if string.casefold() == "exit".casefold():
break
else:
main.update(make_panel(string)) I would like to update my renderable based on some key input with a smooth output. Another example could be navigating an application with ⬆️➡️⬅️⬇️ keys for example. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Rich doesn't have any support for handling keys beyond simple text entry. You may be interested in Rich's sister project, Textual. |
Beta Was this translation helpful? Give feedback.
-
You should check out Here's a very simple example: import keyboard
def pressed_key(keyevent: keyboard.KeyboardEvent):
if keyevent.event_type == keyboard.KEY_DOWN:
do_something()
# "suppress=True" suppresses keyboard events, so it doesn't echo to the terminal
keyboard.hook(pressed_key, suppress=True) |
Beta Was this translation helpful? Give feedback.
Rich doesn't have any support for handling keys beyond simple text entry. You may be interested in Rich's sister project, Textual.