# Keyboard and pointer

> useKeyboard to move a sprite, and three ways to give a sprite its pointer events.

Example `interactive`, running at https://nacatamalon.com/en/examples/interactive. The whole catalogue: https://nacatamalon.com/llms/examples.md.

Drive the sprite with the arrow keys or WASD through `useKeyboard().isDown`. The three bars show the three places a sprite gets its pointer events. The left one has them in `createSprite`'s options. The middle one gets them from `listen()` right after it is created, with no update involved. The red one starts with none: walk the player into it, and a `useUpdate` that notices calls `listen()` once.

`listen()` works anywhere; the update is only there because touching the player is something you have to check every frame. Click the canvas first so it takes keyboard focus.

## Code

`index.ts`:

```ts
import { createGame } from 'nacatamalon';
import * as Settings from './settings';
import { Level } from './scenes/demo';

const game = createGame('#app', {
    width: Settings.WIDTH,
    height: Settings.HEIGHT,
    background: '#10141d',
});

// Declare Scenes
game({
    Level
});
```

`components/button.ts`:

```ts
import { createSprite } from 'nacatamalon';

export const Button = (x: number, idle: number) => {
    const dim = {
        r: 1,
        g: 1,
        b: 1,
        a: idle
    };

    const bright = {
        r: 1,
        g: 1,
        b: 1,
        a: 1
    };

    const button = createSprite({
        tint: dim,
        width: 90,
        height: 44,
        transform: {
            x,
            y: 46,
            rotation: 0,
            scaleX: 1,
            scaleY: 1
        },
        onPointerOver: () => {
            button.tint = bright;
        },
        onPointerOut: () => {
            button.tint = dim;
        },
        // Pressed and released on this same button: dragging off it before letting go does nothing.
        onClick: () => {
            button.transform.rotation += 0.35;
        },
    });
};
```

`components/listenedbutton.ts`:

```ts
import { createSprite, listen } from 'nacatamalon';

export const ListenedButton = (x: number, idle: number) => {
    const dim = {
        r: 1,
        g: 1,
        b: 1,
        a: idle
    };

    const bright = {
        r: 1,
        g: 1,
        b: 1,
        a: 1
    };

    const button = createSprite({
        tint: dim,
        width: 90,
        height: 44,
        transform: {
            x,
            y: 46,
            rotation: 0,
            scaleX: 1,
            scaleY: 1
        },
    });

    listen(button, {
        onPointerOver: () => {
            button.tint = bright;
        },
        onPointerOut: () => {
            button.tint = dim;
        },
        onClick: () => {
            button.transform.rotation += 0.35;
        },
    });
};
```

`components/lockedbutton.ts`:

```ts
import { createSprite, listen, useUpdate } from 'nacatamalon';

import type { TSprite } from 'nacatamalon';

export const LockedButton = (x: number, player: TSprite) => {
    const locked = {
        r: 1,
        g: 0.3,
        b: 0.3,
        a: 0.4
    };

    const dim = {
        r: 1,
        g: 1,
        b: 1,
        a: 0.55
    };

    const bright = {
        r: 1,
        g: 1,
        b: 1,
        a: 1
    };

    const button = createSprite({
        tint: locked,
        width: 90,
        height: 44,
        transform: {
            x,
            y: 46,
            rotation: 0,
            scaleX: 1,
            scaleY: 1
        },
    });

    let unlocked = false;

    useUpdate(() => {
        if (unlocked) {
            return;
        }

        const touching = Math.abs(player.transform.x - button.transform.x) < 69
            && Math.abs(player.transform.y - button.transform.y) < 46;

        if (!touching) {
            return;
        }

        unlocked = true;
        button.tint = dim;
        listen(button, {
            onPointerOver: () => {
                button.tint = bright;
            },
            onPointerOut: () => {
                button.tint = dim;
            },
            onClick: () => {
                button.transform.rotation += 0.35;
            },
        });
    });
};
```

`components/player.ts`:

```ts
import { createSprite, useKeyboard, useLoadTexture, useUpdate } from 'nacatamalon';
import * as Settings from '../settings';

export const Player = () => {
    const texture = useLoadTexture({
        src: '/example-assets/assets/gammix.png',
        key: 'gammix'
    });

    const keys = useKeyboard();

    const player = createSprite({
        zIndex: 1,
        texture,
        width: 48,
        height: 48,
        transform: {
            x: 240,
            y: 180,
            rotation: 0,
            scaleX: 1,
            scaleY: 1
        }
    });

    // The sprite is placed by its middle, so it can go half its size from each edge and no further.
    const HALF = 24;

    useUpdate((delta) => {
        const step = 220 * delta;
        const t = player.transform;

        if (keys.isDown('ArrowLeft') || keys.isDown('a')) {
            t.x -= step;
        }

        if (keys.isDown('ArrowRight') || keys.isDown('d')) {
            t.x += step;
        }

        if (keys.isDown('ArrowUp') || keys.isDown('w')) {
            t.y -= step;
        }

        if (keys.isDown('ArrowDown') || keys.isDown('s')) {
            t.y += step;
        }

        t.x = Math.min(Settings.WIDTH - HALF, Math.max(HALF, t.x));
        t.y = Math.min(Settings.HEIGHT - HALF, Math.max(HALF, t.y));
    });

    return player;
};
```

`scenes/demo.ts`:

```ts
import { createScene, useSpawn } from 'nacatamalon';

import { Player } from '../components/player';
import { Button } from '../components/button';
import { ListenedButton } from '../components/listenedbutton';
import { LockedButton } from '../components/lockedbutton';

export const Level = () => {
    const player = useSpawn(Player)().api;

    useSpawn(Button)(85, 0.35);
    useSpawn(ListenedButton)(240, 0.45);
    useSpawn(LockedButton)(395, player);

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 480;
export const HEIGHT = 320;
```
