Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

docs: rewrite the Input guide to the complete-reference template

yhhc88e3867b4ab
diff --git a/docs/astro/src/content/docs/guides/input.mdx b/docs/astro/src/content/docs/guides/input.mdx
index 5a5321cd9..660705c64 100644
--- a/docs/astro/src/content/docs/guides/input.mdx
+++ b/docs/astro/src/content/docs/guides/input.mdx
@@ -1,12 +1,13 @@
---
title: Input
-description: Keyboard, mouse, touch, and gamepad via the Input resource.
+description: The complete input reference — keyboard, mouse, touch, and gamepad through the Input resource, plus world-space cursor.
---
import { Aside } from '@astrojs/starlight/components';
-Read input from a system through the `Input` resource. It tracks held state plus
-this-frame edges (pressed / released), so query it every frame.
+Read input from a system through the `Input` resource. It tracks **held** state plus
+this-frame **edges** (pressed / released), so query it every frame — the edges are
+only true for the one frame the change happened.
## Keyboard
@@ -20,25 +21,25 @@ const keys = defineSystem([Res(Input)], (input) => {
});
```
-Keys use DOM code strings: `KeyW`, `KeyA`, `ArrowUp`, `Space`, `Escape`,
-`Digit1`, …
+Keys use DOM code strings: `KeyW`, `KeyA`, `ArrowUp`, `Space`, `Escape`, `Digit1`, …
## Mouse
```typescript
const mouse = defineSystem([Res(Input)], (input) => {
if (input.isMouseButtonPressed(0)) { /* left click this frame */ }
- // buttons: 0 = left, 1 = middle, 2 = right
const screenX = input.mouseX, screenY = input.mouseY; // screen pixels
const wheel = input.scrollDeltaY;
});
```
+Buttons: `0` = left, `1` = middle, `2` = right.
+
### Mouse in world space
-`input.mouseX` / `input.mouseY` are screen pixels. For world coordinates — to
-place or hit entities — read `UICameraInfo`, which exposes the cursor already
-projected into the world:
+`input.mouseX` / `mouseY` are screen pixels. For world coordinates — to place or hit
+entities — read `UICameraInfo`, which exposes the cursor already projected into the
+world:
```typescript
import { defineSystem, Commands, Res, Input, UICameraInfo, Transform } from 'esengine';
@@ -56,15 +57,28 @@ const spawnAtCursor = defineSystem(
);
```
-## Touch and gamepad
+## Reference
+
+| Group | Methods |
+|---|---|
+| Keyboard | `isKeyDown(code)`, `isKeyPressed(code)`, `isKeyReleased(code)`. |
+| Mouse | `isMouseButtonDown(b)`, `isMouseButtonPressed(b)`, `isMouseButtonReleased(b)`; `mouseX` / `mouseY`, `scrollDeltaY`. |
+| Touch | `isTouchActive(id)`, the `touches` map of active points. |
+| Gamepad | `isGamepadConnected(pad?)`, `isGamepadButtonDown(btn, pad?)`, `isGamepadButtonPressed`/`Released(btn, pad?)`, `getGamepadAxis(axis, pad?)`. |
+| Pointer | `isPointerOverUI()` — the cursor is over a UI element. |
+
+Each `*Down` reads held state; `*Pressed` / `*Released` are the this-frame edges.
+
+## Best practices
+
+- **Poll every frame** — edges (`*Pressed` / `*Released`) are only set for one frame.
+- **Guard world clicks with `isPointerOverUI()`** so clicking a button doesn't also
+ hit the game world.
+- **Use `UICameraInfo` for world coordinates**, not raw `mouseX/Y`, so picking
+ respects the camera.
+- **Gamepad `pad` defaults to 0** — pass an index for local multiplayer.
-- **Touch** — `input.isTouchActive(id)`, plus the `touches` map of active points.
-- **Gamepad** — `input.isGamepadConnected(pad)`,
- `input.isGamepadButtonDown(button, pad)` (and `isGamepadButtonPressed` /
- `Released`), with `GamepadAxis` for the sticks.
+## See also
-<Aside type="tip">
- `input.isPointerOverUI()` is `true` when the cursor is over a UI element —
- check it before acting on a world click so clicks on buttons don't also hit the
- game world.
-</Aside>
+- [Camera](/docs/guides/camera/) — `screenToWorld` for arbitrary screen points.
+- [UI](/docs/guides/ui/) — UI consumes pointer input above the game world.
diff --git a/docs/astro/src/content/docs/zh-cn/guides/input.mdx b/docs/astro/src/content/docs/zh-cn/guides/input.mdx
index 76e572c3a..3ee277fab 100644
--- a/docs/astro/src/content/docs/zh-cn/guides/input.mdx
+++ b/docs/astro/src/content/docs/zh-cn/guides/input.mdx
@@ -1,12 +1,12 @@
---
title: 输入
-description: 通过 Input 资源读取键盘、鼠标、触摸与手柄。
+description: 输入完整参考——通过 Input 资源读取键盘、鼠标、触摸与手柄,以及世界空间光标。
---
import { Aside } from '@astrojs/starlight/components';
-在系统里通过 `Input` 资源读取输入。它既跟踪按住状态,也跟踪当前帧的边沿(按下 /
-松开),所以每帧查询它。
+在系统里通过 `Input` 资源读取输入。它跟踪**按住**状态以及本帧的**边沿**(按下 / 释放),
+所以每帧查询它——边沿只在变化发生的那一帧为真。
## 键盘
@@ -14,9 +14,9 @@ import { Aside } from '@astrojs/starlight/components';
import { defineSystem, Res, Input } from 'esengine';
const keys = defineSystem([Res(Input)], (input) => {
- if (input.isKeyDown('KeyW')) { /* 按住中 */ }
- if (input.isKeyPressed('Space')) { /* 本帧按下 */ }
- if (input.isKeyReleased('Escape')) { /* 本帧松开 */ }
+ if (input.isKeyDown('KeyW')) { /* held down */ }
+ if (input.isKeyPressed('Space')) { /* pressed this frame */ }
+ if (input.isKeyReleased('Escape')) { /* released this frame */ }
});
```
@@ -26,17 +26,18 @@ const keys = defineSystem([Res(Input)], (input) => {
```typescript
const mouse = defineSystem([Res(Input)], (input) => {
- if (input.isMouseButtonPressed(0)) { /* 本帧左键点击 */ }
- // 按键:0 = 左键,1 = 中键,2 = 右键
- const screenX = input.mouseX, screenY = input.mouseY; // 屏幕像素
+ if (input.isMouseButtonPressed(0)) { /* left click this frame */ }
+ const screenX = input.mouseX, screenY = input.mouseY; // screen pixels
const wheel = input.scrollDeltaY;
});
```
-### 世界坐标下的鼠标
+按键:`0` = 左,`1` = 中,`2` = 右。
-`input.mouseX` / `input.mouseY` 是屏幕像素。要拿世界坐标——用于放置或命中实体——请读
-`UICameraInfo`,它已把光标投影到世界空间:
+### 世界空间里的鼠标
+
+`input.mouseX` / `mouseY` 是屏幕像素。要世界坐标——用于放置或命中实体——读 `UICameraInfo`,
+它暴露已投影到世界里的光标:
```typescript
import { defineSystem, Commands, Res, Input, UICameraInfo, Transform } from 'esengine';
@@ -54,13 +55,26 @@ const spawnAtCursor = defineSystem(
);
```
-## 触摸与手柄
+## 参考
+
+| 分组 | 方法 |
+|---|---|
+| 键盘 | `isKeyDown(code)`、`isKeyPressed(code)`、`isKeyReleased(code)`。 |
+| 鼠标 | `isMouseButtonDown(b)`、`isMouseButtonPressed(b)`、`isMouseButtonReleased(b)`;`mouseX` / `mouseY`、`scrollDeltaY`。 |
+| 触摸 | `isTouchActive(id)`、活动触点的 `touches` map。 |
+| 手柄 | `isGamepadConnected(pad?)`、`isGamepadButtonDown(btn, pad?)`、`isGamepadButtonPressed`/`Released(btn, pad?)`、`getGamepadAxis(axis, pad?)`。 |
+| 指针 | `isPointerOverUI()` —— 光标在 UI 元素上方。 |
+
+`*Down` 读按住状态;`*Pressed` / `*Released` 是本帧边沿。
+
+## 最佳实践
+
+- **每帧轮询**——边沿(`*Pressed` / `*Released`)只在一帧被设置。
+- **用 `isPointerOverUI()` 守卫世界点击**,点按钮时不要也命中游戏世界。
+- **世界坐标用 `UICameraInfo`**,而非裸 `mouseX/Y`,让拾取尊重相机。
+- **手柄 `pad` 默认 0**——本地多人传索引。
-- **触摸** —— `input.isTouchActive(id)`,以及记录活动触点的 `touches` map。
-- **手柄** —— `input.isGamepadConnected(pad)`、`input.isGamepadButtonDown(button, pad)`
- (还有 `isGamepadButtonPressed` / `Released`),摇杆用 `GamepadAxis`。
+## 参见
-<Aside type="tip">
- 当光标位于某个 UI 元素之上时,`input.isPointerOverUI()` 为 `true` —— 在处理世界点击
- 前先检查它,这样点在按钮上的点击就不会同时命中游戏世界。
-</Aside>
+- [相机](/docs/zh-cn/guides/camera/) —— 对任意屏幕点用 `screenToWorld`。
+- [UI](/docs/zh-cn/guides/ui/) —— UI 在游戏世界之上消费指针输入。