## Chapter 12. Hooks

Hooks allow you to specify an action to be executed automatically when some events happen. For example, you can define a hook with actions to be executed when a connection is established, and then these commands will be automatically executed when you connect to the World.

You can connect hooks to seven events:

- `OnConnect`: This hook is executed when a connection to the world is made, after the auto-logon has taken place (if applicable).

- `OnDisconnect`: This hook is executed when you are disconnected from a World, after the connection has been closed.

- `OnReceivedText`: Executed when text is received from the world. The text received is available for the hook in the `$hookdata` variable, and can be inspectd in Perl scripts.

- `OnSentCommand`: Executed after a command is sent to the world. The command is available for the hook in the `$hookdata` variable, and can be inspectd in Perl scripts.

- `OnGetFocus`: Executed when the KildClient window receives the focus.

- `OnLoseFocus`: Executed when the KildClient window loses the focus.

- `OnCloseConnected`: Executed when the world is forcedly closed. This can happen using the World → Close menu, the `$world->close()` function, or by quitting the program with the world connected. You can put in this hook, for example "`quit`" to always exit the MUD nicely, even if you close the world by mistake.

- `OnServerData`: Executed when out-of-band server data is received. See Chapter 16, Out-of-band server data.