This documentation explains how all GameCrom engine controls work: keyboard, multitouch, virtual joystick, mouse and gamepad.
| Key | e.key value |
|---|---|
| Up | ArrowUp |
| Down | ArrowDown |
| Left | ArrowLeft |
| Right | ArrowRight |
| Space | " " |
| Enter | Enter |
| Escape | Escape |
| AโZ | "a" to "z" |
| 0โ9 | "0" to "9" |
Object scripts can implement these functions:
// Key pressed
function OnKeyDown(obj, key) {
Log("โ " + key);
}
// Key held
function OnKeyHold(obj, key) {
Log("โธ " + key);
}
// Key released
function OnKeyUp(obj, key) {
Log("โ " + key);
}
function OnKeyDown(obj, key) {
if (key === "escape") {
let panel = getObjectByName("Panel");
panel.active = !panel.active;
if (panel.active) Pause();
else Resume();
}
}
Each finger becomes a "virtual key":
"touch0""touch1""touch2" โฆThese virtual keys are sent to the same functions:
OnKeyDown(obj, "touch0")
OnKeyHold(obj, "touch0")
OnKeyUp(obj, "touch0")
function OnKeyHold(obj, key) {
// Holding finger on screen
if (key === "touch0") player.y -= 4;
if (key === "touch1") player.y += 4;
}
| Button | Index |
|---|---|
| A (Xbox) / X (PS) | 0 |
| B / O | 1 |
| X / โก | 2 |
| Y / โณ | 3 |
| L1 | 4 |
| R1 | 5 |
| L2 | 6 |
| R2 | 7 |
| Back / Share | 8 |
| Start / Options | 9 |
| Left Stick (button) | 10 |
| Right Stick (button) | 11 |
| Pad Up | 12 |
| Pad Down | 13 |
| Pad Left | 14 |
| Pad Right | 15 |
| Stick | Index | Values |
|---|---|---|
| Left X | 0 | -1 โ 0 โ +1 |
| Left Y | 1 | -1 โ 0 โ +1 |
| Right X | 2 | -1 โ 0 โ +1 |
| Right Y | 3 | -1 โ 0 โ +1 |
const pads = navigator.getGamepads();
let p = pads[0];
if (p) {
let x = p.axes[0];
let y = p.axes[1];
player.x += x * 4;
player.y += y * 4;
if (p.buttons[0].pressed) Jump();
}
The GameCrom engine automatically maps controller buttons and directions to standard names, so you can use them in:
IsAnyPressed()OnKeyDown()OnKeyHold()OnKeyUp()This works the same on Xbox, PlayStation and generic controllers.
| Action | Name |
|---|---|
| Pad Up | gamepad_up |
| Pad Down | gamepad_down |
| Pad Left | gamepad_left |
| Pad Right | gamepad_right |
| Action | Name | Xbox | PlayStation |
|---|---|---|---|
| A Button | gamepad_a | A | X |
| B Button | gamepad_b | B | O |
| X Button | gamepad_x | X | โก |
| Y Button | gamepad_y | Y | โณ |
| Action | Name |
|---|---|
| L1 | gamepad_l1 |
| R1 | gamepad_r1 |
| L2 (trigger) | gamepad_l2 |
| R2 (trigger) | gamepad_r2 |
| Action | Name |
|---|---|
| Start | gamepad_start |
| Back / Share | gamepad_back |
| Left Stick Click | gamepad_ls_click |
| Right Stick Click | gamepad_rs_click |
Sticks are treated as analog axes, but they also have automatically mapped directions for IsAnyPressed:
| Direction | Name |
|---|---|
| Left Stick โ left | gamepad_ls_left |
| Left Stick โ right | gamepad_ls_right |
| Left Stick โ up | gamepad_ls_up |
| Left Stick โ down | gamepad_ls_down |
| Right Stick โ left | gamepad_rs_left |
| Right Stick โ right | gamepad_rs_right |
| Right Stick โ up | gamepad_rs_up |
| Right Stick โ down | gamepad_rs_down |
// Move right with:
// โ Key D
// โ Right arrow
// โ Gamepad right
// โ Left or right stick pushed right
if (IsAnyPressed("d", "ArrowRight",
"gamepad_right",
"gamepad_ls_right",
"gamepad_rs_right")) {
player.x += 5;
}
if (IsAnyPressed(" ", "touch0", "gamepad_a")) {
Jump();
}
if (IsAnyPressed("escape", "gamepad_start")) {
ToggleMenu();
}
IsAnyPressed is an engine API function that checks if
any of the keys, gamepad buttons or touch inputs is pressed in the current frame.
It is the simplest way to mix keyboard, touch and gamepad without handling manual events.
IsAnyPressed(key1, key2, key3, ...)
// Classic WASD + arrow movement
if (IsAnyPressed("ArrowUp", "w")) player.y -= 4;
if (IsAnyPressed("ArrowDown", "s")) player.y += 4;
if (IsAnyPressed("ArrowLeft", "a")) player.x -= 4;
if (IsAnyPressed("ArrowRight", "d")) player.x += 4;
The engineโs multitouch system creates virtual keys:
"touch0", "touch1", "touch2", etc.
// Jump with space or touching the screen
if (IsAnyPressed(" ", "touch0")) {
Jump();
}
if (IsAnyPressed("ArrowRight", "d", "gamepad_right", "touch1")) {
player.x += 5;
}
This lets different input methods perform the same action without duplicating code.
function Update(obj, dt) {
let speed = 200;
// Move left with keyboard, controller or touch
if (IsAnyPressed("ArrowLeft", "a", "gamepad_left", "touch1")) {
obj.x -= speed * dt;
}
// Jump with space, gamepad A or main touch
if (IsAnyPressed(" ", "gamepad_a", "touch0")) {
Jump();
}
}
IsAnyPressed completely simplifies cross-browser and multiplatform control:
PC, mobile, tablet, PWA and controllers.
IsJustPressed is a function of the GameCrom engine API that returns
true only on the exact frame when a key, gamepad button,
or touch input transitions from not pressed to pressed.
It is equivalent to the KeyDown or ButtonDown concept in engines like Unity.
IsJustPressed is NOT held active.true only once, even if the button remains pressed.
IsJustPressed(key1, key2, key3, ...)
// Fire only once when pressing space
if (IsJustPressed(" ")) {
Shoot();
}
---
// Fire only when pressing B (Xbox) / O (PlayStation)
if (IsJustPressed("gamepad_b")) {
Shoot();
}
---
// Action only when the screen is touched for the first time
if (IsJustPressed("touch0")) {
Jump();
}
---
// Jump only once when pressing any of these
if (IsJustPressed(" ", "gamepad_a", "touch0")) {
Jump();
}
---
| Function | When it returns true | Typical use |
|---|---|---|
IsAnyPressed |
While the input is held | Movement, continuous force |
IsJustPressed |
Only on the first frame | Shooting, jumping, menu |
function Update(obj, dt) {
// Continuous movement
if (IsAnyPressed("ArrowRight", "d", "gamepad_right")) {
obj.x += 200 * dt;
}
// Single action
if (IsJustPressed(" ", "gamepad_a", "touch0")) {
Jump();
}
}
IsAnyPressed for continuous actionsIsJustPressed for instant actions.
IsAnyPressed.