@@ -397,10 +397,7 @@ def on_event(self, event: UIEvent) -> Optional[bool]:
397397 # If not active, check to activate, return
398398 if not self ._active and isinstance (event , UIMousePressEvent ):
399399 if self .rect .point_in_rect (event .pos ):
400- self ._active = True
401- self .trigger_full_render ()
402- self .caret .on_activate ()
403- self .caret .position = len (self .doc .text )
400+ self .activate ()
404401 return EVENT_UNHANDLED
405402
406403 # If active check to deactivate
@@ -410,9 +407,7 @@ def on_event(self, event: UIEvent) -> Optional[bool]:
410407 y = int (event .y - self .bottom )
411408 self .caret .on_mouse_press (x , y , event .button , event .modifiers )
412409 else :
413- self ._active = False
414- self .trigger_full_render ()
415- self .caret .on_deactivate ()
410+ self .deactivate ()
416411 return EVENT_UNHANDLED
417412
418413 # If active pass all non press events to caret
@@ -445,6 +440,23 @@ def on_event(self, event: UIEvent) -> Optional[bool]:
445440
446441 return EVENT_UNHANDLED
447442
443+ @property
444+ def active (self ) -> bool :
445+ return self ._active
446+
447+ def activate (self ):
448+ """Programmatically activate the text input field."""
449+ self ._active = True
450+ self .trigger_full_render ()
451+ self .caret .on_activate ()
452+ self .caret .position = len (self .doc .text )
453+
454+ def deactivate (self ):
455+ """Programmatically deactivate the text input field."""
456+ self ._active = False
457+ self .trigger_full_render ()
458+ self .caret .on_deactivate ()
459+
448460 def _update_layout (self ):
449461 # Update Pyglet layout size
450462 layout = self .layout
0 commit comments