From 61ccb8948bbced4ea49806894774295bf195a1b5 Mon Sep 17 00:00:00 2001 From: Dave Pearson Date: Mon, 12 Dec 2022 09:55:14 +0000 Subject: [PATCH] :sparkles: Mouse capture test To answer someone's question in Discord. --- mouse_capture.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 mouse_capture.py diff --git a/mouse_capture.py b/mouse_capture.py new file mode 100644 index 0000000..f154a2e --- /dev/null +++ b/mouse_capture.py @@ -0,0 +1,43 @@ +from textual.app import App, ComposeResult +from textual.containers import Vertical +from textual.widgets import Static, TextLog +from textual.events import MouseMove, Key + +class MouseArea( Static ): + + def on_mount( self ) -> None: + self.capture_mouse() + self.screen.query_one( TextLog ).write( "Mouse area mounted" ) + + def on_mouse_move( self, event: MouseMove ) -> None: + self.screen.query_one( TextLog ).write( f"Mouse move: {event!r}" ) + +class Report( TextLog ): + pass + +class MouseApp( App[ None ] ): + + CSS = """ + MouseArea { + height: 1fr; + background: #005500; + } + Report { + height: 1fr; + background: #888888; + color: black; + } + """ + + def compose( self ) -> ComposeResult: + yield Vertical( + MouseArea(), + Report() + ) + + def on_key( self, event: Key ) -> None: + if event.key.isdecimal(): + self.query_one( TextLog ).write( f"Key: {event!r}" ) + +if __name__ == "__main__": + MouseApp().run()