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.

feat(input): MouseButton.Left / Middle / Right

yhhb8b1f1f4a54a
diff --git a/docs/astro/src/content/docs/scripting/input.mdx b/docs/astro/src/content/docs/scripting/input.mdx
index 462a27973..e0ad17c97 100644
--- a/docs/astro/src/content/docs/scripting/input.mdx
+++ b/docs/astro/src/content/docs/scripting/input.mdx
@@ -125,7 +125,7 @@ length 1):
| Binding | Feeds | Meaning |
|---|---|---|
| `Key(code)` | button / axis | One keyboard key. |
-| `MouseButton(b)` | button / axis | A mouse button. |
+| `MouseButton(b)` | button / axis | A mouse button (`MouseButton.Left` / `.Middle` / `.Right` = 0 / 1 / 2 — the same names work in `input.isMouseButtonDown`). |
| `GpButton(btn, pad?)` | button / axis | A gamepad button (analog value for triggers). |
| `GpAxis(axis, pad?, scale?)` | axis | A raw gamepad axis. |
| `Keys1D(neg, pos)` | axis | Two keys → a signed 1D axis. |
diff --git a/docs/astro/src/content/docs/zh-cn/scripting/input.mdx b/docs/astro/src/content/docs/zh-cn/scripting/input.mdx
index 441975143..85fd47f5b 100644
--- a/docs/astro/src/content/docs/zh-cn/scripting/input.mdx
+++ b/docs/astro/src/content/docs/zh-cn/scripting/input.mdx
@@ -116,7 +116,7 @@ if (Controls.pressed('Jump')) { /* 本帧边沿 */ }
| 绑定 | 供给 | 含义 |
|---|---|---|
| `Key(code)` | button / axis | 一个键盘键。 |
-| `MouseButton(b)` | button / axis | 一个鼠标键。 |
+| `MouseButton(b)` | button / axis | 一个鼠标键(`MouseButton.Left` / `.Middle` / `.Right` = 0 / 1 / 2,同样可直接用于 `input.isMouseButtonDown`)。 |
| `GpButton(btn, pad?)` | button / axis | 一个手柄键(扳机给模拟值)。 |
| `GpAxis(axis, pad?, scale?)` | axis | 一根原始手柄轴。 |
| `Keys1D(neg, pos)` | axis | 两个键 → 带符号的 1D 轴。 |
diff --git a/sdk/src/input/inputMap.ts b/sdk/src/input/inputMap.ts
index fc04f88f6..505099eae 100644
--- a/sdk/src/input/inputMap.ts
+++ b/sdk/src/input/inputMap.ts
@@ -80,9 +80,16 @@ const INPUT_MAP_ASSET_VERSION = 1;
* it types, so WASD stays WASD on AZERTY.
* @public */
export const Key = (code: string): Binding => ({ kind: 'key', code });
-/** A mouse button, by the DOM numbering (0 = left, 1 = middle, 2 = right).
+/** A mouse button, by the DOM numbering (0 = left, 1 = middle, 2 = right) — and
+ * those three by name, so `MouseButton.Left` reads the same in a binding
+ * (`MouseButton(MouseButton.Left)`) and in a direct query
+ * (`input.isMouseButtonDown(MouseButton.Left)`). Before the names existed
+ * `MouseButton.Left` was undefined, and a query with it was never true.
* @public */
-export const MouseButton = (button: number): Binding => ({ kind: 'mouse', button });
+export const MouseButton = Object.assign(
+ (button: number): Binding => ({ kind: 'mouse', button }),
+ { Left: 0, Middle: 1, Right: 2 } as const,
+);
/** A gamepad button — see {@link GamepadButton}. `pad` defaults to the first one.
* @public */
export const GpButton = (button: number, pad?: number): Binding => ({ kind: 'gpButton', button, pad });
diff --git a/sdk/tests/input-map.test.ts b/sdk/tests/input-map.test.ts
index 3cbce5c6f..5873e017d 100644
--- a/sdk/tests/input-map.test.ts
+++ b/sdk/tests/input-map.test.ts
@@ -116,3 +116,10 @@ describe('InputMap', () => {
expect(map2.actions()).toEqual(['Jump']);
});
});
+
+describe('MouseButton names', () => {
+ it('names the three DOM buttons, and the constructor still builds a binding', () => {
+ expect([MouseButton.Left, MouseButton.Middle, MouseButton.Right]).toEqual([0, 1, 2]);
+ expect(MouseButton(MouseButton.Right)).toEqual({ kind: 'mouse', button: 2 });
+ });
+});