> For the complete documentation index, see [llms.txt](https://easy-draw.joemazzone.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://easy-draw.joemazzone.net/python/module-reference/event-handling.md). Agents can also search and read this documentation through its [MCP server](https://easy-draw.joemazzone.net/~gitbook/mcp).

# Event Handling

## Events List

Easy Draw uses Tkinter-style event names. The browser sends these events to your program while it is running. Here is a detailed list of event formats:

* `<Button-1>`
  * A mouse button is pressed over the object. Button 1 is the leftmost button, button 2 is the middle button (where available), and button 3 the rightmost button. The current position of the mouse pointer (relative to the canvas) is provided in the x and y members of the event object passed to the handler.
  * You can use ButtonPress instead of Button, or even leave it out completely: `<Button-1>`, `<ButtonPress-1>`, and `<1>` are all synonyms. For clarity, I prefer the `<Button-1>` syntax.
* `<B1-Motion>`
  * The mouse is moved, with mouse button 1 being held down (use B2 for the middle button, B3 for the right button). The current position of the mouse pointer is provided in the x and y members of the event object passed to the handler.
* `<Motion>`
  * The mouse is moved over the object.
* `<ButtonRelease-1>`
  * Button 1 was released. The current position of the mouse pointer is provided in the x and y members of the event object passed to the handler.
* `<Double-Button-1>`
  * Button 1 was double clicked. You can use Double or Triple as prefixes. If you bind both a single click (`<Button-1>`) and a double click, the double click runs the `<Double-Button-1>` handler instead of a second `<Button-1>`.
* `<Enter>`
  * The mouse pointer entered the object (this event doesn’t mean that the user pressed the Enter key!).
* `<Leave>`
  * The mouse pointer left the object.
* `<MouseWheel>`
  * The mouse wheel was turned. How far it moved is in the delta member of the event object.
* `<FocusIn>`
  * Keyboard focus was moved to the Easy Draw page.
* `<FocusOut>`
  * Keyboard focus was moved from the Easy Draw page to another window or tab.
* `<Return>`
  * The user pressed the Enter key. You can bind to virtually all keys on the keyboard. The special keys are BackSpace, Tab, Return (the Enter key), Escape, Shift\_L / Shift\_R, Control\_L / Control\_R, Alt\_L / Alt\_R, Caps\_Lock, Prior (Page Up), Next (Page Down), End, Home, Left, Up, Right, Down, Insert, Delete, and F1 through F12.
* `<Key>`
  * The user pressed any key. The key is provided in the char member of the event object passed to the handler (this is an empty string for special keys).
* `<KeyRelease>`
  * The user let go of a key. You can add a key name, like `<KeyRelease-Up>`.
* `a`
  * The user typed an “a”. Most printable characters can be used as is. The exceptions are space (`<space>`) and less than (`<less>`). Note that `1` is a keyboard binding, while `<1>` is a button binding.
* `<Shift-Up>`
  * The user pressed the Up arrow, while holding the Shift key pressed. You can use prefixes like Alt, Shift, and Control.

{% hint style="info" %}
Key presses only reach Easy Draw when the Easy Draw page is the active browser tab. Click on the page first if your key events are not working.
{% endhint %}

Each of these events have useful properties that can be retrieved in the event handler function:

* `x`, `y`
  * The current mouse position on the canvas, in pixels.
* `x_root`, `y_root`
  * The current mouse position relative to the upper left corner of the screen, in pixels.
* `char`
  * The character typed (keyboard events only), as a string.
* `keysym`
  * The key symbol, like `"a"`, `"Up"`, or `"space"` (keyboard events only).
* `keycode`
  * The key code (keyboard events only).
* `num`
  * The button number (mouse button events only).
* `delta`
  * How far the mouse wheel moved (mouse wheel events only).
* `type`
  * The event type, like `"ButtonPress"` or `"KeyPress"`.

## Event Handlers

To create an event handler, you simply just create a function definition and provide it with `event` as a parameter. Inside the definition, you provide the statements you wish to be executed when the event is triggered.

**Example:**

```python
def click_octagon(event):
    global octagon
    color = random.choice(["blue", "red", "yellow", "green", "orange", "purple"])
    octagon.set_property(color=color)
    octagon.rotate(10)

octagon.event_setup("<Button-1>", click_octagon)
```

Event handlers run one at a time, in the order the events happen.

## Binding an Event to an Easy Draw Object

`object.event_setup(event, handler)`

To attach or bind an event and handler to an Easy Draw object you must use the `.event_setup()` method and provide the event name and handler function name as parameters.

**Example:** Bind a `<Button-1>` event with handler named `click_octagon` to the object stored in `octagon`.

```python
octagon.event_setup("<Button-1>", click_octagon)
```

## Binding an Event to the Canvas

`canvas_event_setup(event, handler)`

To attach or bind an event and handler to the entire Easy Draw canvas you must use the `canvas_event_setup()` function and provide the event name and handler function name as parameters.

*It is recommended to bind keyboard events to the entire canvas instead of binding key events to a specific object.*

**Example:** Bind a `<Up>` arrow key event with handler named `move_up` .

```python
easy_draw.canvas_event_setup("<Up>", move_up)
```

## Animations

You can animate a drawing with a loop and `time.sleep()`. The browser shows each change while the loop runs.

```python
import easy_draw
import time

easy_draw.load_canvas()

ball = easy_draw.Circle(
    center_xy = (50, 300),
    radius = 25,
    color = "red"
)

for i in range(50):
    ball.center_xy = (ball.center_xy[0] + 10, 300)
    time.sleep(0.05)

easy_draw.end()
```
