# Hear clicks

> Let your program react when the user clicks its resident, with `pixling resident events`.

Let your script react whenever the user clicks its resident. [`pixling resident events`](/docs/reference/residents/#resident-events-cli-only) streams one JSON line per click while it runs. Pixling keeps no backlog, so clicks while nothing is listening are not delivered.

> [!NOTE]
> **Prerequisites**
> Turn on **CLI connection** in **Settings** › **Connect**, and bind a resident with [`pixling resident ensure`](/docs/reference/resident-binding/). The examples below use `my-agent/conversation-1`:
>
> 
```sh title="Terminal"
pixling resident ensure --namespace my-agent --key conversation-1 --asset-id No.7
```

## Try it by hand

Start streaming events from your resident:

```sh title="Terminal"
pixling resident events --to client:my-agent/conversation-1
```

Click the resident to watch lines arrive in Terminal, and press `Ctrl`+`C` to stop.

| Event line | When it prints |
| --- | --- |
| `{"event":"click"}` | Every plain click on the resident while listening. |
| `{"event":"ended","reason":"residentStowed"}` | When the resident is put away; the command then exits `0`. |

## React in a script

Pipe `pixling resident events` into a shell loop to run your own action on each click:

```sh title="Terminal"
pixling resident events --to client:my-agent/conversation-1 --exclusive |
  while read -r line; do
    case "$line" in
      '{"event":"click"}') pixling message send --to client:my-agent/conversation-1 --text "You clicked me" ;;
    esac
  done
```

### Exclusive click handling

| Mode | Resident's click reaction and shortcut | Hover reaction | Delivered to your script |
| --- | --- | --- | --- |
| **Default** (no flag) | Plays and opens as usual | Unchanged | Yes |
| **`--exclusive`** | Suppressed while your script listens | Unchanged | Yes |

- Only a plain click on the resident counts. Pixling does not watch other input, other residents, or other apps.
- The loop ends when the resident is put away or when you stop the pipeline.
- Stopping the pipeline with `Ctrl`+`C` restores normal click behavior right away.

> [!CAUTION]
> **Stop the whole pipeline, not just the loop**
> With `--exclusive`, if only the right-hand loop exits (for example via `break` or `head -1`), `pixling` does not detect the closed pipe until the next click, which then does nothing. Stop the entire pipeline instead.

## Next steps

- [Follow a window](/docs/connect/follow-a-window/): Keep a resident perched on another app's window with pixling resident follow.
- [Resident events reference](/docs/reference/residents/#resident-events-cli-only): Flags, event payloads, and exit behavior for pixling resident events.
