keys command

This commit is contained in:
Will McGugan
2022-12-20 10:36:00 +00:00
parent c20bf664e8
commit 18d38f6a4d
3 changed files with 48 additions and 1 deletions

View File

@@ -123,3 +123,11 @@ def colors():
from textual.cli.previews import colors
colors.app.run()
@run.command("keys")
def keys():
"""Show key events"""
from textual.cli.previews import keys
keys.app.run()

View File

@@ -0,0 +1,37 @@
from rich.panel import Panel
from textual.app import App, ComposeResult
from textual import events
from textual.widgets import Header, Footer, TextLog
class KeyLog(TextLog, inherit_bindings=False):
"""We don't want to handle scroll keys."""
class KeysApp(App):
"""Show key events in a text log."""
TITLE = "Textual Keys"
BINDINGS = [("c", "clear", "Clear")]
def compose(self) -> ComposeResult:
yield Header()
yield Footer()
yield KeyLog()
def on_ready(self) -> None:
self.query_one(KeyLog).write(Panel("Press some keys!"))
def on_key(self, event: events.Key) -> None:
self.query_one(KeyLog).write(event)
def action_clear(self) -> None:
self.query_one(KeyLog).clear()
app = KeysApp()
if __name__ == "__main__":
app.run()

View File

@@ -209,7 +209,9 @@ class Key(InputEvent):
def __rich_repr__(self) -> rich.repr.Result:
yield "key", self.key
yield "char", self.char, None
yield "char", self.char
yield "is_printable", self.is_printable
yield "key_aliases", self.key_aliases, [self.key_name]
@property
def key_name(self) -> str | None: