Input System

Keyboard, mouse, and raycast input — available from any script component.

Keyboard Input

js
import { Keyboard, KeyCode } from "kernelplay-js";

// Held down this frame
if (Keyboard.isPressed(KeyCode.ArrowRight)) { rb.velocity.x = 200; }

// Pressed exactly this frame (good for jumps/actions)
if (Keyboard.wasPressed(KeyCode.Space)) { rb.velocity.y = -400; }

// Released this frame
if (Keyboard.wasReleased(KeyCode.Escape)) { pauseGame(); }

// Common key Code:
// Arrow keys:  ArrowUp, ArrowDown, ArrowLeft, ArrowRight
// WASD:        W, A, S, D
// Misc:        Space, Enter, Escape, ShiftLeft, ShiftRight
// Any key:     Use the KeyboardEvent.key value directly

Mouse Input

js
import { Mouse, MouseButton } from "kernelplay-js";

// Position (canvas-relative)
const x = Mouse.x;
const y = Mouse.y;

// Buttons: 0 = left, 1 = middle, 2 = right
if (Mouse.isPressed(MouseButton.Left))    { /* holding left button */ }
if (Mouse.wasPressed(MouseButton.Left))   { /* clicked this frame */ }
if (Mouse.wasReleased(MouseButton.Right))  { /* 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 */ } }