API Reference /Input

API Reference

Input

Keyboard, mouse and touch. Queries to poll every frame and events that fire once per action.

Import
import * as input from "codetoy/input"
// Events are functions you export: onKeyDown, onMouseDown, onTouchStart, ...
// Input is a global static class; no using needed.
Input.OnKeyDown((string key) => { });
from codetoy._input import mouseX, mouseY, isMouseDown, isKeyDown
# Events are functions you define: keyDown, mouseDown, touchStart, ...

For when to poll and when to use events, see the Handling Input guide.

#Keyboard

#isKeyDown

Returns truetrueTrue while key is held down. key uses the browser's KeyboardEvent.key names: "a", "ArrowUp", " ", "Enter", "Shift".

input.isKeyDown(key: string): bool
Input.IsKeyDown(string key) // → bool
isKeyDown(key: str) -> bool

#onKeyDown / onKeyUp

Called once when a key is pressed or released.

export function onKeyDown(key: string): void {
  if (key == " ") jump()
}

export function onKeyUp(key: string): void { }
Input.OnKeyDown((string key) => {
    if (key == " ") Jump();
});

Input.OnKeyUp((string key) => { });
def keyDown(key):
    if key == " ":
        jump()

def keyUp(key):
    pass

#Mouse

#mouseX / mouseY

The pointer's position in canvas pixels, updated as the mouse moves.

input.mouseX(): f64
input.mouseY(): f64
Input.MouseX // double field
Input.MouseY // double field
mouseX() -> float
mouseY() -> float

#isMouseDown

Returns truetrueTrue while button is held: 0 left, 1 middle, 2 right.

input.isMouseDown(button: i32): bool
Input.IsMouseDown(int button) // → bool
isMouseDown(button: int) -> bool

#onMouseDown / onMouseUp

Called once when a mouse button is pressed or released.

export function onMouseDown(button: i32): void {
  if (button == 0) shoot(input.mouseX(), input.mouseY())
}

export function onMouseUp(button: i32): void { }
Input.OnMouseDown((int button) => {
    if (button == 0) Shoot(Input.MouseX, Input.MouseY);
});

Input.OnMouseUp((int button) => { });
def mouseDown(button):
    if button == 0:
        shoot(mouseX(), mouseY())

def mouseUp(button):
    pass

#Touch

Each finger on the screen has an id that stays the same from touch start to touch end.

#touchCount

The number of fingers currently on the screen.

input.touchCount(): i32
Input.TouchCount() // → int
touchCount() -> int

#isTouchDown

With no argument, returns truetrueTrue if any finger is down. With an id, returns whether that finger is still down.

input.isTouchDown(id: i32 = -1): bool
Input.IsTouchDown(int id = -1) // → bool
isTouchDown(id: int = -1) -> bool

#touchX / touchY

The current position of finger id, or -1 if it isn't down.

input.touchX(id: i32): f64
input.touchY(id: i32): f64
Input.TouchX(int id) // → double
Input.TouchY(int id) // → double
touchX(id: int) -> float
touchY(id: int) -> float

#onTouchStart / onTouchMove / onTouchEnd

Called when a finger lands, moves or lifts. Positions are in canvas pixels.

export function onTouchStart(id: i32, x: f64, y: f64): void { }
export function onTouchMove(id: i32, x: f64, y: f64): void { }
export function onTouchEnd(id: i32): void { }
Input.OnTouchStart((int id, double x, double y) => { });
Input.OnTouchMove((int id, double x, double y) => { });
Input.OnTouchEnd((int id) => { });
def touchStart(id, x, y): ...
def touchMove(id, x, y): ...
def touchEnd(id): ...
Esc