@@ -2977,11 +2977,20 @@ def _binding_chain(self) -> list[tuple[DOMNode, _Bindings]]:
29772977
29782978 return namespace_bindings
29792979
2980- async def check_bindings (self , key : str , priority : bool = False ) -> bool :
2980+ def simulate_key (self , key : str ) -> None :
2981+ """Simulate a key press.
2982+
2983+ This will perform the same action as if the user had pressed the key.
2984+
2985+ Args:
2986+ key: Key to simulate. May also be the name of a key, e.g. "space".
2987+ """
2988+ self .call_later (self ._check_bindings , key )
2989+
2990+ async def _check_bindings (self , key : str , priority : bool = False ) -> bool :
29812991 """Handle a key press.
29822992
2983- This method is used internally by the bindings system, but may be called directly
2984- if you wish to *simulate* a key being pressed.
2993+ This method is used internally by the bindings system.
29852994
29862995 Args:
29872996 key: A key.
@@ -3049,7 +3058,7 @@ async def on_event(self, event: events.Event) -> None:
30493058 self .screen ._clear_tooltip ()
30503059 except NoScreen :
30513060 pass
3052- if not await self .check_bindings (event .key , priority = True ):
3061+ if not await self ._check_bindings (event .key , priority = True ):
30533062 forward_target = self .focused or self .screen
30543063 forward_target ._forward_event (event )
30553064 else :
@@ -3138,7 +3147,7 @@ async def run_action(
31383147 return False
31393148
31403149 async def _dispatch_action (
3141- self , namespace : object , action_name : str , params : Any
3150+ self , namespace : DOMNode , action_name : str , params : Any
31423151 ) -> bool :
31433152 """Dispatch an action to an action method.
31443153
@@ -3175,6 +3184,7 @@ async def _dispatch_action(
31753184 except SkipAction :
31763185 # The action method raised this to explicitly not handle the action
31773186 log .system (f"<action> { action_name !r} skipped." )
3187+
31783188 return False
31793189
31803190 async def _broker_event (
@@ -3230,7 +3240,7 @@ async def _on_layout(self, message: messages.Layout) -> None:
32303240 message .stop ()
32313241
32323242 async def _on_key (self , event : events .Key ) -> None :
3233- if not (await self .check_bindings (event .key )):
3243+ if not (await self ._check_bindings (event .key )):
32343244 await self .dispatch_key (event )
32353245
32363246 async def _on_shutdown_request (self , event : events .ShutdownRequest ) -> None :
@@ -3461,14 +3471,15 @@ def _watch_app_focus(self, focus: bool) -> None:
34613471 # Remove focus for now.
34623472 self .screen .set_focus (None )
34633473
3464- async def action_check_bindings (self , key : str ) -> None :
3465- """An [action](/guide/actions) to handle a key press using the binding system.
3474+ async def action_simulate_key (self , key : str ) -> None :
3475+ """An [action](/guide/actions) to simulate a key press.
3476+
3477+ This will invoke the same actions as if the user had pressed the key.
34663478
34673479 Args:
34683480 key: The key to process.
34693481 """
3470- if not await self .check_bindings (key , priority = True ):
3471- await self .check_bindings (key , priority = False )
3482+ self .simulate_key (key )
34723483
34733484 async def action_quit (self ) -> None :
34743485 """An [action](/guide/actions) to quit the app as soon as possible."""
0 commit comments