Event Handlers
Register UI Automation event handlers from Python. The register_* methods on
AutomationElement and
AutomationBase return an EventRegistration handle that keeps the callback
alive and can unregister it (also usable as a context manager).
def on_invoked(element, event_id):
print("invoked!")
registration = button.register_automation_event(
button.patterns.invoke.pattern.raw_pattern.EventIds.InvokedEvent,
TreeScope.Element,
on_invoked,
)
# ... later ...
registration.unregister()
flaui.core.event_handlers.EventRegistration(cs_handler, callback, unregister)
Handle to a registered UI Automation event; keeps the callback alive and allows unregistering.
Use it as a context manager or call :meth:unregister / :meth:dispose when done.
Store the C# handler and callback and add the registration to the keep-alive registry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cs_handler
|
Any
|
The C# event-handler object returned by the native |
required |
callback
|
Callable[..., None]
|
The Python callback bridged to C# (kept alive to prevent GC). |
required |
unregister
|
Callable[[Any], None]
|
A callable that unregisters |
required |
Source code in flaui/core/event_handlers.py
is_active
property
Return whether the registration is still active.
Returns:
| Type | Description |
|---|---|
bool
|
|
raw_handler
property
Return the underlying C# event-handler object.
Returns:
| Type | Description |
|---|---|
Any
|
The native handler returned by the |
__enter__()
Return self for use as a context manager.
Returns:
| Type | Description |
|---|---|
'EventRegistration'
|
This registration. |
__exit__(*exc)
Unregister the handler when leaving the with block.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exc
|
Any
|
Exception info (unused). |
()
|
dispose()
unregister()
Unregister the event handler from C# and drop the keep-alive reference.