# Event Handling

## Events List

Easy Draw supports all Tkinter events.  Here is a detailed list if event formats:

* `<Button-1>`
  * A mouse button is pressed over the widget. Button 1 is the leftmost button, button 2 is the middle button (where available), and button 3 the rightmost button. When you press down a mouse button over a widget, Tkinter will automatically “grab” the mouse pointer, and subsequent mouse events (e.g. Motion and Release events) will then be sent to the current widget as long as the mouse button is held down, even if the mouse is moved outside the current widget. The current position of the mouse pointer (relative to the widget) is provided in the x and y members of the event object passed to the callback.
  * 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 callback.
* `<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 callback.
* `<Double-Button-1>`
  * Button 1 was double clicked. You can use Double or Triple as prefixes. Note that if you bind to both a single click (\<Button-1>) and a double click, both bindings will be called.
* `<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.
* `<FocusIn>`
  * Keyboard focus was moved to Easy Draw.
* `<FocusOut>`
  * Keyboard focus was moved from Easy Draw to another Window.
* `<Return>`
  * The user pressed the Enter key. You can bind to virtually all keys on the keyboard. For an ordinary 102-key PC-style keyboard, the special keys are Cancel (the Break key), BackSpace, Tab, Return(the Enter key), Shift\_L (any Shift key), Control\_L (any Control key), Alt\_L (any Alt key), Pause, Caps\_Lock, Escape, Prior (Page Up), Next (Page Down), End, Home, Left, Up, Right, Down, Print, Insert, Delete, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, Num\_Lock, and Scroll\_Lock.
* `<Key>`
  * The user pressed any key. The key is provided in the char member of the event object passed to the callback (this is an empty string for special keys).
* `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.

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

* `x`, `y`
  * The current mouse position, in pixels.
* `x_root`, `y_root`
  * The current mouse position relative to the upper left corner of the screen, in pixels.
* `char`
  * The character code (keyboard events only), as a string.
* `keysym`
  * The key symbol (keyboard events only).
* `keycode`
  * The key code (keyboard events only).
* `num`
  * The button number (mouse button events only).
* `type`
  * The event type.

## 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.&#x20;

**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)
```

## 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.&#x20;

**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.&#x20;

*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)
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://easy-draw.joemazzone.net/python/module-reference/event-handling.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
