> 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/java/module-reference/event-handling.md).

# Event Handling

## Events List

The Java version uses the same event names as the Python version, like `"<Button-1>"`, `"<B1-Motion>"`, `"<Key>"`, and `"<Up>"`. See the full list here:

{% content-ref url="/pages/-MRAvSb83fmXckFG2fA6" %}
[Event Handling](https://easy-draw.joemazzone.net/python/module-reference/event-handling.md)
{% endcontent-ref %}

{% 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 %}

## Event Handlers

An event handler is the code that runs when the event happens. Write it as a lambda: `event -> { ... }`

```java
square.eventSetup("<Button-1>", event -> {
    square.setColor("blue");
    square.rotate(10);
});
```

If the handler is only one statement, you can leave out the curly braces:

```java
square.eventSetup("<Button-1>", event -> square.rotate(10));
```

You can also write the handler as a `static` method that takes an `EasyDraw.Event`, and pass it with `ClassName::methodName`:

```java
public class Main {
    static EasyDraw.Rectangle square;

    public static void main(String[] args) {
        EasyDraw.loadCanvas();

        square = new EasyDraw.Rectangle(100, 100, 200, 200);
        square.eventSetup("<Button-1>", Main::clickSquare);

        EasyDraw.end();
    }

    static void clickSquare(EasyDraw.Event event) {
        square.rotate(10);
    }
}
```

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

### Changing Variables in a Handler

A lambda cannot change a local variable from `main`. Use a `static` variable of your class instead:

```java
public class Main {
    static int clicks = 0;

    public static void main(String[] args) {
        EasyDraw.loadCanvas();

        EasyDraw.Text counter = new EasyDraw.Text(300, 300, "Clicks: 0");
        EasyDraw.canvasEventSetup("<Button-1>", event -> {
            clicks = clicks + 1;
            counter.setText("Clicks: " + clicks);
        });

        EasyDraw.end();
    }
}
```

## Event Information

The `event` passed to your handler has useful information about the event:

| Field                        | Description                                                              |
| ---------------------------- | ------------------------------------------------------------------------ |
| `event.x`, `event.y`         | The mouse position on the canvas, in pixels.                             |
| `event.xRoot`, `event.yRoot` | The mouse position on the screen, in pixels.                             |
| `event.character`            | The character typed, as a `String` (keyboard events only).               |
| `event.keysym`               | The key symbol, like `"a"`, `"Up"`, or `"space"` (keyboard events only). |
| `event.keycode`              | The key code (keyboard events only).                                     |
| `event.num`                  | The mouse button number (mouse button events only).                      |
| `event.delta`                | How far the mouse wheel moved (mouse wheel events only).                 |
| `event.type`                 | The event type, like `"ButtonPress"` or `"KeyPress"`.                    |

```java
EasyDraw.canvasEventSetup("<Button-1>", event -> {
    System.out.println("You clicked at " + event.x + ", " + event.y);
});
```

## Binding an Event to an Easy Draw Object

`object.eventSetup(event, handler)`

```java
octagon.eventSetup("<Button-1>", event -> octagon.rotate(10));
```

## Binding an Event to the Canvas

`EasyDraw.canvasEventSetup(event, handler)`

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

```java
EasyDraw.canvasEventSetup("<Up>", event -> player.setCenter(player.getCenterX(), player.getCenterY() - 10));
```

## Animations

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

```java
public class Main {
    public static void main(String[] args) throws InterruptedException {
        EasyDraw.loadCanvas();

        EasyDraw.Circle ball = new EasyDraw.Circle(50, 300, 25);
        ball.setColor("red");

        for (int i = 0; i < 50; i++) {
            ball.setCenter(ball.getCenterX() + 10, 300);
            Thread.sleep(50);
        }

        EasyDraw.end();
    }
}
```
