docs: clarify key_* methods vs on_key for App-level key handling#6619
Open
twall wants to merge 1 commit into
Open
docs: clarify key_* methods vs on_key for App-level key handling#6619twall wants to merge 1 commit into
twall wants to merge 1 commit into
Conversation
key_* methods on App are called directly by the internal key dispatcher regardless of whether a child widget stopped the event. on_key handlers on App do NOT receive keys that were stopped by a focused widget (e.g. Input). This distinction is important and was previously undocumented. - input.md: expand the key methods note with the practical implication and a concrete example - events.md: add a tip in 'Stopping bubbling' cross-referencing key_* methods as the solution when Input consumes keys
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The docs describe
key_*methods as "a convenience for experimenting" and say bindings/actions are "nearly always preferable." This misses a critical use case and leaves developers confused.on_keyonAppdoes not receive key events that were stopped by a focused widget (e.g.Input). This is becauseon_keyrelies on event bubbling, andInputcallsevent.stop()on keys it handles.key_*methods onAppdo fire regardless — they're called directly byApp._on_keyviadispatch_key(), before bubbling. This makes them the correct tool for App-level keys that must work regardless of what widget has focus.This distinction is non-obvious and took significant debugging to discover. Neither
input.mdnorevents.mdmentioned it.Changes
input.md: Expands the key methods note with the practical implication and a concrete example showing theisinstance(self.focused, Input)guard pattern.events.md: Adds a tip in the "Stopping bubbling" section cross-referencingkey_*methods as the solution.Example