Input
The Input class manages keyboard and mouse input for a game or application by registering event listeners on a given canvas element. It provides methods to check for held and pressed keys, as well as mouse positions and button presses.
Static Properties
Section titled “Static Properties”Input.KEYS
Section titled “Input.KEYS”Input.KEYS;A constant object mapping key names to their string representations. This includes common keyboard keys such as alphanumeric keys, special keys, and function keys.
Example:
Section titled “Example:”Input.KEYS.ENTER; // "Enter"Input.KEYS.ARROW_LEFT; // "ArrowLeft"Input.MOUSE
Section titled “Input.MOUSE”Input.MOUSE;A constant object mapping mouse buttons to their numerical values.
LEFT(0)MIDDLE(1)RIGHT(2)
Example:
Section titled “Example:”Input.MOUSE.LEFT; // 0Input.MOUSE.RIGHT; // 2Constructor
Section titled “Constructor”new Input(canvas)
Section titled “new Input(canvas)”Initializes the Input object, attaches event listeners for keyboard and mouse events to the provided canvas element.
- Parameters:
canvas(HTMLCanvasElement): The canvas element to capture input events from.
Example:
Section titled “Example:”const input = new Input(document.getElementById('gameCanvas'));Methods
Section titled “Methods”initEventListeners()
Section titled “initEventListeners()”Sets up event listeners for keydown, keyup, mousemove, mousedown, and mouseup events. Automatically called in the constructor.
- Returns:
void
isKeyHeld(key)
Section titled “isKeyHeld(key)”Checks if a key is currently being held down.
-
Parameters:
key(String): The key to check (case-insensitive).
-
Returns:
Boolean:trueif the key is being held down,falseotherwise.
Example:
Section titled “Example:”if (input.isKeyHeld(Input.KEYS.ARROW_UP)) { // Move player up}isKeyPressed(key)
Section titled “isKeyPressed(key)”Checks if a key was pressed once (and resets the key state to prevent continuous firing).
-
Parameters:
key(String): The key to check (case-insensitive).
-
Returns:
Boolean:trueif the key was pressed,falseotherwise.
Example:
Section titled “Example:”if (input.isKeyPressed(Input.KEYS.ENTER)) { // Confirm selection}getMousePosition()
Section titled “getMousePosition()”Returns the current position of the mouse relative to the canvas.
- Returns:
Object: An object withxandyproperties representing the mouse’s position.
Example:
Section titled “Example:”const mousePos = input.getMousePosition();console.log(`Mouse X: ${mousePos.x}, Mouse Y: ${mousePos.y}`);isMouseButtonHeld(button)
Section titled “isMouseButtonHeld(button)”Checks if a specific mouse button is currently being held down.
-
Parameters:
button(Number): The mouse button to check (e.g.,Input.MOUSE.LEFT).
-
Returns:
Boolean:trueif the button is being held down,falseotherwise.
Example:
Section titled “Example:”if (input.isMouseButtonHeld(Input.MOUSE.LEFT)) { // Dragging logic}isMouseButtonPressed(button)
Section titled “isMouseButtonPressed(button)”Checks if a specific mouse button was pressed once (and resets the button state).
-
Parameters:
button(Number): The mouse button to check (e.g.,Input.MOUSE.LEFT).
-
Returns:
Boolean:trueif the button was pressed,falseotherwise.
Example:
Section titled “Example:”if (input.isMouseButtonPressed(Input.MOUSE.LEFT)) { // Select tile}