# Device events

`device.events`: live accessibility events, as callbacks or one-shot waits. Each kind of event
is its own class. Guide: [Device events](../../guide/events.md).

## <kbd>class</kbd> `DeviceEvents`
Live accessibility events from the device (``Device.events``): Android tells the driver when a toast shows, a window changes, a view is clicked or scrolled, or text or content changes, and the server streams the kinds a registration asks for. Each kind is its own event class, and the class is what you subscribe with, so a callback or a wait gets that class and its fields:
``` 

     device.events.on_toast(lambda e: print(e.text))           # e: ToastShownEvent      device.events.wait_for(WindowChangedEvent, package="com.shop") 

```
Events are hints, not proof: Android merges and drops them. React to one by reading the screen again (``device.screen.element(...)``); never assume the event alone says the screen is ready. Watching is read-only and changes nothing on the device. 

### <kbd>method</kbd> `DeviceEvents.__init__`

```python
__init__(device: 'Device') → None
```

---

### <kbd>method</kbd> `DeviceEvents.expect`

```python
expect(
    kind: 'type[E]',
    where: 'Callable[[E], bool] | None' = None,
    package: 'str | None' = None
) → ExpectedEvent[E]
```

Subscribes now for the first event of ``kind`` that ``where`` accepts (every event when it is None), to ``wait`` for after an action — the event cannot slip past between the action and the wait:
``` 

     with device.events.expect(ToastShownEvent, where=lambda e: "Saved" in e.text) as saved:          app.element(text("Save")).tap()          toast = saved.wait(timeout=5) 

---

### <kbd>method</kbd> `DeviceEvents.on_click`

```python
on_click(
    callback: 'Callable[[ViewClickedEvent], object]',
    package: 'str | None' = None
) → EventRegistration[ViewClickedEvent]
```

``register(ViewClickedEvent, callback)``: every clicked view. 

---

### <kbd>method</kbd> `DeviceEvents.on_content_change`

```python
on_content_change(
    callback: 'Callable[[ContentChangedEvent], object]',
    package: 'str | None' = None
) → EventRegistration[ContentChangedEvent]
```

``register(ContentChangedEvent, callback)``: every change inside a window (frequent). 

---

### <kbd>method</kbd> `DeviceEvents.on_scroll`

```python
on_scroll(
    callback: 'Callable[[ViewScrolledEvent], object]',
    package: 'str | None' = None
) → EventRegistration[ViewScrolledEvent]
```

``register(ViewScrolledEvent, callback)``: every scroll. 

---

### <kbd>method</kbd> `DeviceEvents.on_text_change`

```python
on_text_change(
    callback: 'Callable[[TextChangedEvent], object]',
    package: 'str | None' = None
) → EventRegistration[TextChangedEvent]
```

``register(TextChangedEvent, callback)``: every edit of an editable view's text. 

---

### <kbd>method</kbd> `DeviceEvents.on_toast`

```python
on_toast(
    callback: 'Callable[[ToastShownEvent], object]',
    package: 'str | None' = None
) → EventRegistration[ToastShownEvent]
```

``register(ToastShownEvent, callback)``: every toast shown. 

---

### <kbd>method</kbd> `DeviceEvents.on_window_change`

```python
on_window_change(
    callback: 'Callable[[WindowChangedEvent], object]',
    package: 'str | None' = None
) → EventRegistration[WindowChangedEvent]
```

``register(WindowChangedEvent, callback)``: every window that appears, goes away or changes. 

---

### <kbd>method</kbd> `DeviceEvents.register`

```python
register(
    kind: 'type[E]',
    callback: 'Callable[[E], object]',
    package: 'str | None' = None
) → EventRegistration[E]
```

Calls ``callback`` with every event of ``kind`` (an event class such as ``ToastShownEvent``) from now on, on a background thread, until the returned registration is closed or the device detached. When this returns the server is subscribed: every event an action started afterwards causes is delivered. ``package`` keeps only that app's. 

Registrations are independent of each other; at most 64 per attached device. Use one as a context manager to close it, and to have its failures raised, at the end of a block:
``` 

     with device.events.register(WindowChangedEvent, print):          app.element(text("Settings")).tap() 

---

### <kbd>method</kbd> `DeviceEvents.wait_for`

```python
wait_for(
    kind: 'type[E]',
    where: 'Callable[[E], bool] | None' = None,
    package: 'str | None' = None,
    timeout: 'float | None' = None
) → E
```

Blocks for the first event of ``kind`` that ``where`` accepts, from now on, up to ``timeout`` seconds (default: ``timeouts.wait``). Only events that fire after the call count: to wait for what an action causes, use ``expect`` before the action. 

## <kbd>class</kbd> `EventRegistration`
A live registration made by ``DeviceEvents.register`` (or an ``on_*`` helper). Its callback runs on one background thread of its own, once per matching event, in the order the events fired, until ``close()``, the device's detach, or a failure that ends it (``error``). 

A callback that raises is logged and does not stop the registration; the first such exception is kept in ``callback_error``. Used as a context manager, the registration is closed when the block exits, and if it ended with an ``error`` or a callback raised, the block re-raises that (unless the block itself raised):
``` 

     with device.events.on_toast(lambda e: toasts.append(e.text)):          checkout.tap() 

### <kbd>method</kbd> `EventRegistration.__init__`

```python
__init__(
    device: 'Device',
    kind: 'type[E]',
    callback: 'Callable[[E], object]',
    package: 'str | None'
) → None
```

---

#### <kbd>property</kbd> EventRegistration.active

True until it is closed or ended (detach, ``error``). 

---

#### <kbd>property</kbd> EventRegistration.callback_error

The first exception the callback raised; None when it never did. 

---

#### <kbd>property</kbd> EventRegistration.error

What ended the registration when it was not closed or detached: ``ServerError`` with ``RESOURCE_EXHAUSTED`` when events were lost (the callback fell more than 1024 events behind, or the device produced them faster than they could be sent), ``SESSION_UNUSABLE`` when the driver connection was lost. None otherwise. 

---

### <kbd>method</kbd> `EventRegistration.close`

```python
close(timeout: 'float' = 5.0) → None
```

Stops delivery and waits up to ``timeout`` seconds for a callback in progress to return (not when called from the callback itself). Idempotent. 

## <kbd>class</kbd> `ExpectedEvent`
A one-shot wait made by ``DeviceEvents.expect``: subscribed when it was created, so an event the next action causes cannot be missed. ``wait`` returns the first matching event. Close it (or use it as a context manager) when it is no longer needed. 

### <kbd>method</kbd> `ExpectedEvent.__init__`

```python
__init__(
    device: 'Device',
    kind: 'type[E]',
    where: 'Callable[[E], bool] | None',
    package: 'str | None',
    default_timeout: 'float'
) → None
```

---

### <kbd>method</kbd> `ExpectedEvent.close`

```python
close() → None
```

Unsubscribes; idempotent. 

---

### <kbd>method</kbd> `ExpectedEvent.wait`

```python
wait(timeout: 'float | None' = None) → E
```

The first matching event, waiting up to ``timeout`` seconds (default: the device's ``timeouts.wait``) from this call. Raises ``WaitTimeoutError`` when none arrived, the registration's ``error`` when events were lost or the driver connection was, the predicate's exception when ``where`` raised, and ``TapError`` when the device was detached first. 

## <kbd>class</kbd> `DeviceEvent`
What every accessibility event has; each kind is a subclass (``ToastShownEvent``, ``WindowChangedEvent``, ...). A hint that the screen changed, not the screen's state: Android merges and drops events, and an event holds no element. To act on one, read the screen again through a selector. 

### <kbd>method</kbd> `DeviceEvent.__init__`

```python
__init__(sequence: 'int', uptime_ms: 'int', package_name: 'str | None') → None
```

## <kbd>class</kbd> `ToastShownEvent`
A toast was shown, as it happened. (``ToastEvent`` is a different thing: an entry of ``Device.toast_history``, which keeps the last toasts for reading later.) 

### <kbd>method</kbd> `ToastShownEvent.__init__`

```python
__init__(
    sequence: 'int',
    uptime_ms: 'int',
    package_name: 'str | None',
    text: 'str'
) → None
```

## <kbd>class</kbd> `WindowChangedEvent`
A window appeared, went away or changed: an activity, dialog, popup or pane. 

### <kbd>method</kbd> `WindowChangedEvent.__init__`

```python
__init__(
    sequence: 'int',
    uptime_ms: 'int',
    package_name: 'str | None',
    class_name: 'str | None',
    title: 'str | None',
    window_id: 'int | None',
    changes: 'frozenset[WindowChange]'
) → None
```

## <kbd>class</kbd> `ContentChangedEvent`
Content inside a window changed. Frequent: an animation can raise one per frame. 

### <kbd>method</kbd> `ContentChangedEvent.__init__`

```python
__init__(
    sequence: 'int',
    uptime_ms: 'int',
    package_name: 'str | None',
    class_name: 'str | None',
    window_id: 'int | None',
    changes: 'frozenset[ContentChange]'
) → None
```

## <kbd>class</kbd> `ViewClickedEvent`
A view was clicked, by Tap or by anything else. 

### <kbd>method</kbd> `ViewClickedEvent.__init__`

```python
__init__(
    sequence: 'int',
    uptime_ms: 'int',
    package_name: 'str | None',
    class_name: 'str | None',
    label: 'str | None',
    window_id: 'int | None'
) → None
```

## <kbd>class</kbd> `ViewScrolledEvent`
A view scrolled. The values a view does not report are None. 

### <kbd>method</kbd> `ViewScrolledEvent.__init__`

```python
__init__(
    sequence: 'int',
    uptime_ms: 'int',
    package_name: 'str | None',
    class_name: 'str | None',
    window_id: 'int | None',
    scroll_x: 'int | None',
    scroll_y: 'int | None',
    max_scroll_x: 'int | None',
    max_scroll_y: 'int | None',
    delta_x: 'int | None',
    delta_y: 'int | None',
    from_index: 'int | None',
    to_index: 'int | None',
    item_count: 'int | None'
) → None
```

## <kbd>class</kbd> `TextChangedEvent`
An editable view's text changed. ``text`` and ``before_text`` hold what was typed (at most 512 characters each); Android masks a password field's text itself. Android raises it for typed input, once per key (``type_text``); text set in one step (``set_text``) is a ``ContentChangedEvent`` with ``ContentChange.TEXT`` instead. 

### <kbd>method</kbd> `TextChangedEvent.__init__`

```python
__init__(
    sequence: 'int',
    uptime_ms: 'int',
    package_name: 'str | None',
    class_name: 'str | None',
    text: 'str',
    before_text: 'str',
    from_index: 'int | None',
    added_count: 'int | None',
    removed_count: 'int | None',
    password: 'bool',
    window_id: 'int | None'
) → None
```

## <kbd>class</kbd> `WindowChange`
What changed about a window (``WindowChangedEvent.changes``). 

## <kbd>class</kbd> `ContentChange`
What changed inside a window (``ContentChangedEvent.changes``). 

