Input System
Keyboard, mouse, and raycast input — available from any script component.
Keyboard Input
js
import { Keyboard } from "kernelplay-js";
// Held down this frame
if (Keyboard.isPressed("ArrowRight")) { rb.velocity.x = 200; }
// Pressed exactly this frame (good for jumps/actions)
if (Keyboard.wasPressed("Space")) { rb.velocity.y = -400; }
// Released this frame
if (Keyboard.wasReleased("Escape")) { pauseGame(); }
// Common key strings:
// Arrow keys: "ArrowUp" "ArrowDown" "ArrowLeft" "ArrowRight"
// WASD: "w" "a" "s" "d" (lowercase)
// Misc: "Space" "Enter" "Escape" "ShiftLeft" "ShiftRight"
// Any key: Use the KeyboardEvent.key value directly
Mouse Input
js
import { Mouse } from "kernelplay-js";
// Position (canvas-relative)
const x = Mouse.x;
const y = Mouse.y;
// Buttons: 0 = left, 1 = middle, 2 = right
if (Mouse.isPressed(0)) { /* holding left button */ }
if (Mouse.wasPressed(0)) { /* clicked this frame */ }
if (Mouse.wasReleased(2)) { /* right button released */ }
// Delta movement since last frame
const dx = Mouse.deltaX;
const dy = Mouse.deltaY;
// Scroll wheel
const scroll = Mouse.scrollDelta;
Raycasting from Mouse
Use the scene's raycast() method with the mouse position to pick entities.
js
class ClickToSelect extends ScriptComponent {
update(dt) {
if (Mouse.wasPressed(0)) {
const hit = this.entity.scene.raycast(Mouse.x, Mouse.y);
if (hit) {
console.log("Clicked entity:", hit.entity.name);
hit.entity.getComponent("renderer").color = "yellow";
}
}
}
}
Gamepad (Experimental)
js
import { Gamepad } from "kernelplay-js";
// Analog sticks — returns -1 to 1
const lx = Gamepad.axis(0); // left stick X
const ly = Gamepad.axis(1); // left stick Y
// Buttons
if (Gamepad.wasPressed("A")) { jump(); }
if (Gamepad.isPressed("RT")) { { /* right trigger */ } }