> ## Documentation Index
> Fetch the complete documentation index at: https://klyx.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Events

Klyx uses an event bus that plugins can subscribe to. You can react to file opens, terminal events, and more.

## Getting the event bus

From within a `KlyxPlugin` lifecycle method:

```kotlin theme={null}
val bus = currentPluginContext().eventBus
```

Or using the service delegate:

```kotlin theme={null}
// Inject via by runtime() or access from PluginContext
```

## Subscribing to events

```kotlin theme={null}
val job = pluginScope.launch {
    bus.subscribe(FileOpenedEvent::class) { event ->
        showToast("Opened: ${event.fileName}")
    }
}

// Cancel subscription when plugin unloads
job.cancel()
```

## Built-in events

### FileOpenedEvent

Published when a file is opened in the editor.

```kotlin theme={null}
data class FileOpenedEvent(
    val uri: Uri,
    val fileName: String,
    val tabId: String,
    val projectUri: Uri?
)
```

### NewSessionEvent

Published when a new terminal session is created.

```kotlin theme={null}
data class NewSessionEvent(
    val id: Uuid,
    val session: TerminalSession
)
```

### SessionTerminateEvent

Published when a terminal session is terminated.

```kotlin theme={null}
data class SessionTerminateEvent(
    val id: Uuid
)
```

### TerminateAllSessionEvent

Published when all terminal sessions are terminated.

```kotlin theme={null}
data object TerminateAllSessionEvent
```

### TerminalNotificationTapEvent

Published when a terminal notification is tapped.

```kotlin theme={null}
data object TerminalNotificationTapEvent
```

## Publishing custom events

```kotlin theme={null}
bus.publish(MyCustomEvent())
```

## Complete example

```kotlin theme={null}
override suspend fun onLoad() {
    val bus = currentPluginContext().eventBus

    // Subscribe to events
    pluginScope.launch {
        bus.subscribe<FileOpenedEvent> { event ->
            showToast("File opened: ${event.fileName}")
        }
        bus.subscribe(NewSessionEvent::class) { event ->
            showToast("New terminal: ${event.id}")
        }
        bus.subscribe(TerminateAllSessionEvent::class) {
            showToast("All terminals closed")
        }
    }
}
```
