React to any remaining key (non bound ones) #3631
pawamoy
started this conversation in
Show and tell
Replies: 2 comments 5 replies
-
Adjacent to #2269 too, I'd say. While not directly related, this feels like something to keep in mind if/when doing a full review of the binding system. |
Beta Was this translation helpful? Give feedback.
1 reply
-
As an aside, if you don't mind a sneaky dip into one internal on from textual.app import App, ComposeResult
from textual.containers import VerticalScroll
from textual.events import Key
from textual.screen import ModalScreen
from textual.widgets import Static
class AnyKeyScreen(ModalScreen[None]):
def compose(self) -> ComposeResult:
with VerticalScroll():
yield Static(
"\n".join([
f"{n} on_key and no Binding make Jack a dull boy"
for n in range(1000)
])
)
def on_key(self, event: Key) -> None:
if not any(bindings.keys.get(event.key) for _, bindings in self.app._binding_chain):
self.dismiss()
class AnyKeyDismissApp(App[None]):
def on_mount(self) -> None:
self.push_screen(AnyKeyScreen())
if __name__ == "__main__":
AnyKeyDismissApp().run() Could also be that |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I sometimes push a modal that reacts to scroll keys (up, down, left, right, pageup, pagedown, home, end), and wanted to bind any remaining key to a "dismiss" action.
Turns out it's quite easy to do:
Could be nice-to-haves in Textual itself:
bindings
attribute that always returns actualBinding
instances, and not tupleskeys
attribute onBinding
that does the comma splitThis way
bound_keys
could be rewritten:...and maybe this could become part of Textual too 🙂
Beta Was this translation helpful? Give feedback.
All reactions