Input

Collect input from the user's keyboard and mouse.

keyDown()

Check if this is the first frame a keyboard key is pressed.

Example
Press x on your keyboard to see the canvas blink once.

newGame('canvas-id');

gameLoop(() => {
    if(keyDown('x')) {
        canvasClearColor(255, 0, 0);
    } else {
        canvasClearColor(0, 0, 0);
    }
});

Overloads
keyDown(key: string, repeat: boolean = false): boolean

Parameters
key: string: A keyboard key. See this list for possible keys.

Return
true if the key was pressed this frame, false otherwise.


keysDown()

When a key is pressed, you get all keys currently pressed.

Example
Press some keys on your keyboard.

newGame('canvas-id');

let lines = [];

gameLoop(() => {
    if(keysDown().length) {
        lines.unshift(keysDown().join(', '));
    }

    for(let i = 0; i < lines.length; i++) {
        drawText(lines[i], 10, i * 20 + 10);
    }
});

Overloads
keyDown(): string[]

Parameters
No parameters

Return
Every key currently held down.


mousePos()

Retrieve the current mouse position relative to the top left of the <canvas>.

Example

newGame('canvas-id');

gameLoop(() => {
    const m = mousePos();
    color(255, 250, 100);
    drawRect('fill', m.x - 20, m.y - 20, 40, 40);
});

Overloads
mousePos(): { x: number, y: number }

Parameters
No parameters

Return
The current mouse position as an object with x and y coordinate fields relative to the top left of the<canvas>.