From b40825e042df659288fa83fae09ec5fb6712c30f Mon Sep 17 00:00:00 2001 From: Will Eastcott Date: Tue, 29 Apr 2025 15:47:34 +0100 Subject: [PATCH 1/7] Convert top-level constants to enums --- src/deprecated/deprecated.js | 6 ++++++ src/index.js | 1 + src/platform/input/constants.js | 28 ---------------------------- src/platform/input/controller.js | 2 +- src/platform/input/mouse-event.js | 12 +++--------- src/platform/input/mouse.js | 25 +++++++------------------ 6 files changed, 18 insertions(+), 56 deletions(-) diff --git a/src/deprecated/deprecated.js b/src/deprecated/deprecated.js index 2cecc7ac067..b4643b69f3a 100644 --- a/src/deprecated/deprecated.js +++ b/src/deprecated/deprecated.js @@ -45,6 +45,7 @@ import { AssetRegistry } from '../framework/asset/asset-registry.js'; import { XrInputSource } from '../framework/xr/xr-input-source.js'; import { ElementInput } from '../framework/input/element-input.js'; +import { MouseButton } from '../platform/input/mouse-button.js'; import { MouseEvent } from '../platform/input/mouse-event.js'; import { AppBase } from '../framework/app-base.js'; @@ -771,6 +772,11 @@ export const EVENT_SELECT = 'select'; export const EVENT_SELECTSTART = 'selectstart'; export const EVENT_SELECTEND = 'selectend'; +export const MOUSEBUTTON_NONE = MouseButton.None; +export const MOUSEBUTTON_LEFT = MouseButton.Left; +export const MOUSEBUTTON_MIDDLE = MouseButton.Middle; +export const MOUSEBUTTON_RIGHT = MouseButton.Right; + Object.defineProperty(ElementInput.prototype, 'wheel', { get: function () { return this.wheelDelta * -2; diff --git a/src/index.js b/src/index.js index 236cb540b60..eb648af79f8 100644 --- a/src/index.js +++ b/src/index.js @@ -144,6 +144,7 @@ export { GamePads } from './platform/input/game-pads.js'; export { Keyboard } from './platform/input/keyboard.js'; export { KeyboardEvent } from './platform/input/keyboard-event.js'; export { Mouse } from './platform/input/mouse.js'; +export { MouseButton } from './platform/input/mouse-button.js'; export { MouseEvent } from './platform/input/mouse-event.js'; export { TouchDevice } from './platform/input/touch-device.js'; export { getTouchTargetCoords, Touch, TouchEvent } from './platform/input/touch-event.js'; diff --git a/src/platform/input/constants.js b/src/platform/input/constants.js index d6df3bb2d33..84e7db5a119 100644 --- a/src/platform/input/constants.js +++ b/src/platform/input/constants.js @@ -592,34 +592,6 @@ export const KEY_CLOSE_BRACKET = 221; */ export const KEY_META = 224; -/** - * No mouse buttons pressed. - * - * @category Input - */ -export const MOUSEBUTTON_NONE = -1; - -/** - * The left mouse button. - * - * @category Input - */ -export const MOUSEBUTTON_LEFT = 0; - -/** - * The middle mouse button. - * - * @category Input - */ -export const MOUSEBUTTON_MIDDLE = 1; - -/** - * The right mouse button. - * - * @category Input - */ -export const MOUSEBUTTON_RIGHT = 2; - /** * Index for pad 1. * diff --git a/src/platform/input/controller.js b/src/platform/input/controller.js index 77e4779a7a4..bfe36a9b0cb 100644 --- a/src/platform/input/controller.js +++ b/src/platform/input/controller.js @@ -159,7 +159,7 @@ class Controller { * @param {object} action - An action object to add. * @param {ACTION_KEYBOARD | ACTION_MOUSE | ACTION_GAMEPAD} action.type - The name of the action. * @param {number[]} [action.keys] - Keyboard: A list of keycodes e.g. `[pc.KEY_A, pc.KEY_ENTER]`. - * @param {number} [action.button] - Mouse: e.g. `pc.MOUSEBUTTON_LEFT` - Gamepad: e.g. `pc.PAD_FACE_1` + * @param {number} [action.button] - Mouse: e.g. `pc.MouseButton.LEFT` - Gamepad: e.g. `pc.PAD_FACE_1` * @param {number} [action.pad] - Gamepad: An index of the pad to register (use {@link PAD_1}, etc). */ appendAction(action_name, action) { diff --git a/src/platform/input/mouse-event.js b/src/platform/input/mouse-event.js index 230d4636fb8..3a973bb2fad 100644 --- a/src/platform/input/mouse-event.js +++ b/src/platform/input/mouse-event.js @@ -1,5 +1,3 @@ -import { MOUSEBUTTON_NONE } from './constants.js'; - /** * @import { Mouse } from './mouse.js' */ @@ -54,15 +52,11 @@ class MouseEvent { dy = 0; /** - * The mouse button associated with this event. Can be: - * - * - {@link MOUSEBUTTON_LEFT} - * - {@link MOUSEBUTTON_MIDDLE} - * - {@link MOUSEBUTTON_RIGHT} + * The mouse button associated with this event. * - * @type {number} + * @type {MouseButton} */ - button = MOUSEBUTTON_NONE; + button = MouseButton.NONE; /** * A value representing the amount the mouse wheel has moved, only valid for diff --git a/src/platform/input/mouse.js b/src/platform/input/mouse.js index 0b694e23d67..ce495e36fba 100644 --- a/src/platform/input/mouse.js +++ b/src/platform/input/mouse.js @@ -3,6 +3,10 @@ import { EventHandler } from '../../core/event-handler.js'; import { isMousePointerLocked, MouseEvent } from './mouse-event.js'; +/** + * @import { MouseButton } from './mouse-button.js'; + */ + /** * @callback LockMouseCallback * Callback used by {@link Mouse#enablePointerLock} and {@link Mouse#disablePointerLock}. @@ -245,12 +249,7 @@ class Mouse extends EventHandler { /** * Returns true if the mouse button is currently pressed. * - * @param {number} button - The mouse button to test. Can be: - * - * - {@link MOUSEBUTTON_LEFT} - * - {@link MOUSEBUTTON_MIDDLE} - * - {@link MOUSEBUTTON_RIGHT} - * + * @param {MouseButton} button - The mouse button to test. * @returns {boolean} True if the mouse button is current pressed. */ isPressed(button) { @@ -260,12 +259,7 @@ class Mouse extends EventHandler { /** * Returns true if the mouse button was pressed this frame (since the last call to update). * - * @param {number} button - The mouse button to test. Can be: - * - * - {@link MOUSEBUTTON_LEFT} - * - {@link MOUSEBUTTON_MIDDLE} - * - {@link MOUSEBUTTON_RIGHT} - * + * @param {MouseButton} button - The mouse button to test. * @returns {boolean} True if the mouse button was pressed since the last update. */ wasPressed(button) { @@ -275,12 +269,7 @@ class Mouse extends EventHandler { /** * Returns true if the mouse button was released this frame (since the last call to update). * - * @param {number} button - The mouse button to test. Can be: - * - * - {@link MOUSEBUTTON_LEFT} - * - {@link MOUSEBUTTON_MIDDLE} - * - {@link MOUSEBUTTON_RIGHT} - * + * @param {MouseButton} button - The mouse button to test. * @returns {boolean} True if the mouse button was released since the last update. */ wasReleased(button) { From 81974423ef3b4e755097f1d1818a46c9969fdb43 Mon Sep 17 00:00:00 2001 From: Will Eastcott Date: Tue, 29 Apr 2025 15:49:12 +0100 Subject: [PATCH 2/7] Add MouseButton declaration --- src/platform/input/mouse-button.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/platform/input/mouse-button.js diff --git a/src/platform/input/mouse-button.js b/src/platform/input/mouse-button.js new file mode 100644 index 00000000000..b0b1d1c1fab --- /dev/null +++ b/src/platform/input/mouse-button.js @@ -0,0 +1,20 @@ +/** + * Mouse buttons. + * + * @readonly + * @enum {ValueOf} + * @category Input + */ +export const MouseButton = Object.freeze({ + /** No specific projection format is used. */ + None: -1, + + /** The left mouse button. */ + Left: 0, + + /** The middle mouse button. */ + Middle: 1, + + /** The right mouse button. */ + Right: 2 +}); From 55452119f11acea0658a91324c95b2bd4a097b33 Mon Sep 17 00:00:00 2001 From: Will Eastcott Date: Tue, 29 Apr 2025 15:59:35 +0100 Subject: [PATCH 3/7] Fix tests --- src/platform/input/mouse-event.js | 4 +++- test/platform/input/mouse.test.mjs | 8 ++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/platform/input/mouse-event.js b/src/platform/input/mouse-event.js index 3a973bb2fad..3305686bcda 100644 --- a/src/platform/input/mouse-event.js +++ b/src/platform/input/mouse-event.js @@ -1,3 +1,5 @@ +import { MouseButton } from './mouse-button.js'; + /** * @import { Mouse } from './mouse.js' */ @@ -7,7 +9,7 @@ * * @returns {boolean} True if pointer lock is currently enabled. */ -function isMousePointerLocked() { +const isMousePointerLocked = () => { return !!(document.pointerLockElement || document.mozPointerLockElement || document.webkitPointerLockElement); } diff --git a/test/platform/input/mouse.test.mjs b/test/platform/input/mouse.test.mjs index 1e1b10197de..f7be2f47d49 100644 --- a/test/platform/input/mouse.test.mjs +++ b/test/platform/input/mouse.test.mjs @@ -1,9 +1,9 @@ import { expect } from 'chai'; -import { MOUSEBUTTON_LEFT, MOUSEBUTTON_MIDDLE, MOUSEBUTTON_RIGHT } from '../../../src/platform/input/constants.js'; import { Mouse } from '../../../src/platform/input/mouse.js'; +import { MouseButton } from '../../../src/platform/input/mouse-button.js'; -const buttons = [MOUSEBUTTON_LEFT, MOUSEBUTTON_MIDDLE, MOUSEBUTTON_RIGHT]; +const buttons = [MouseButton.Left, MouseButton.Middle, MouseButton.Right]; // Mock the _getTargetCoords method, otherwise it returns null Mouse.prototype._getTargetCoords = function (event) { @@ -59,7 +59,7 @@ describe('Mouse', function () { it('should handle mousedown events', (done) => { mouse.on('mousedown', (event) => { - expect(event.button).to.equal(MOUSEBUTTON_LEFT); + expect(event.button).to.equal(MouseButton.Left); expect(event.event).to.be.an.instanceOf(MouseEvent); done(); @@ -71,7 +71,7 @@ describe('Mouse', function () { it('should handle mouseup events', (done) => { mouse.on('mouseup', (event) => { - expect(event.button).to.equal(MOUSEBUTTON_LEFT); + expect(event.button).to.equal(MouseButton.Left); expect(event.event).to.be.an.instanceOf(MouseEvent); done(); From adbee9d0ca7408a325d506bd93192a782323b288 Mon Sep 17 00:00:00 2001 From: Will Eastcott Date: Tue, 29 Apr 2025 16:00:20 +0100 Subject: [PATCH 4/7] Fix comment --- src/platform/input/mouse-button.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/platform/input/mouse-button.js b/src/platform/input/mouse-button.js index b0b1d1c1fab..3d5c46305fa 100644 --- a/src/platform/input/mouse-button.js +++ b/src/platform/input/mouse-button.js @@ -6,7 +6,7 @@ * @category Input */ export const MouseButton = Object.freeze({ - /** No specific projection format is used. */ + /** No button. */ None: -1, /** The left mouse button. */ From 80c5ce8db259b95bbbc901e444d3a73511a35d01 Mon Sep 17 00:00:00 2001 From: Will Eastcott Date: Tue, 29 Apr 2025 16:02:22 +0100 Subject: [PATCH 5/7] Lint fixes --- src/platform/input/mouse-event.js | 2 +- test/platform/input/mouse.test.mjs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/platform/input/mouse-event.js b/src/platform/input/mouse-event.js index 3305686bcda..c93528d9eee 100644 --- a/src/platform/input/mouse-event.js +++ b/src/platform/input/mouse-event.js @@ -11,7 +11,7 @@ import { MouseButton } from './mouse-button.js'; */ const isMousePointerLocked = () => { return !!(document.pointerLockElement || document.mozPointerLockElement || document.webkitPointerLockElement); -} +}; /** * The MouseEvent object is passed into all event handlers registered on the {@link Mouse}. The diff --git a/test/platform/input/mouse.test.mjs b/test/platform/input/mouse.test.mjs index f7be2f47d49..7c7bca5b1dc 100644 --- a/test/platform/input/mouse.test.mjs +++ b/test/platform/input/mouse.test.mjs @@ -1,7 +1,7 @@ import { expect } from 'chai'; -import { Mouse } from '../../../src/platform/input/mouse.js'; import { MouseButton } from '../../../src/platform/input/mouse-button.js'; +import { Mouse } from '../../../src/platform/input/mouse.js'; const buttons = [MouseButton.Left, MouseButton.Middle, MouseButton.Right]; From 9c578a1b04d40c464af4ae374f50c4f9b0f2c1ce Mon Sep 17 00:00:00 2001 From: Will Eastcott Date: Tue, 29 Apr 2025 16:42:34 +0100 Subject: [PATCH 6/7] Fix docs --- src/platform/input/mouse-button.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/platform/input/mouse-button.js b/src/platform/input/mouse-button.js index 3d5c46305fa..75314f1a78f 100644 --- a/src/platform/input/mouse-button.js +++ b/src/platform/input/mouse-button.js @@ -2,7 +2,7 @@ * Mouse buttons. * * @readonly - * @enum {ValueOf} + * @enum {typeof MouseButton[keyof typeof MouseButton]} * @category Input */ export const MouseButton = Object.freeze({ From 1a78db1ef2a321e1f0557104b7f1a047d48e1b25 Mon Sep 17 00:00:00 2001 From: Will Eastcott Date: Tue, 29 Apr 2025 17:00:40 +0100 Subject: [PATCH 7/7] Make key contants into enum --- src/deprecated/deprecated.js | 99 +++++ src/framework/app-base.js | 2 +- src/framework/xr/xr-manager.js | 2 +- src/platform/input/constants.js | 582 --------------------------- src/platform/input/controller.js | 6 +- src/platform/input/keyboard-event.js | 7 +- src/platform/input/keyboard.js | 14 +- 7 files changed, 117 insertions(+), 595 deletions(-) diff --git a/src/deprecated/deprecated.js b/src/deprecated/deprecated.js index b4643b69f3a..e9c3b501598 100644 --- a/src/deprecated/deprecated.js +++ b/src/deprecated/deprecated.js @@ -45,6 +45,7 @@ import { AssetRegistry } from '../framework/asset/asset-registry.js'; import { XrInputSource } from '../framework/xr/xr-input-source.js'; import { ElementInput } from '../framework/input/element-input.js'; +import { Key } from '../platform/input/key.js'; import { MouseButton } from '../platform/input/mouse-button.js'; import { MouseEvent } from '../platform/input/mouse-event.js'; @@ -772,6 +773,104 @@ export const EVENT_SELECT = 'select'; export const EVENT_SELECTSTART = 'selectstart'; export const EVENT_SELECTEND = 'selectend'; +export const KEY_BACKSPACE = Key.Backspace; +export const KEY_TAB = Key.Tab; +export const KEY_RETURN = Key.Return; +export const KEY_ENTER = Key.Enter; +export const KEY_SHIFT = Key.Shift; +export const KEY_CONTROL = Key.Control; +export const KEY_ALT = Key.Alt; +export const KEY_PAUSE = Key.Pause; +export const KEY_CAPS_LOCK = Key.CapsLock; +export const KEY_ESCAPE = Key.Escape; +export const KEY_SPACE = Key.Space; +export const KEY_PAGE_UP = Key.PageUp; +export const KEY_PAGE_DOWN = Key.PageDown; +export const KEY_END = Key.End; +export const KEY_HOME = Key.Home; +export const KEY_LEFT = Key.Left; +export const KEY_UP = Key.Up; +export const KEY_RIGHT = Key.Right; +export const KEY_DOWN = Key.Down; +export const KEY_PRINT_SCREEN = Key.PrintScreen; +export const KEY_INSERT = Key.Insert; +export const KEY_DELETE = Key.Delete; +export const KEY_0 = Key.Key0; +export const KEY_1 = Key.Key1; +export const KEY_2 = Key.Key2; +export const KEY_3 = Key.Key3; +export const KEY_4 = Key.Key4; +export const KEY_5 = Key.Key5; +export const KEY_6 = Key.Key6; +export const KEY_7 = Key.Key7; +export const KEY_8 = Key.Key8; +export const KEY_9 = Key.Key9; +export const KEY_SEMICOLON = Key.Semicolon; +export const KEY_EQUAL = Key.Equal; +export const KEY_A = Key.A; +export const KEY_B = Key.B; +export const KEY_C = Key.C; +export const KEY_D = Key.D; +export const KEY_E = Key.E; +export const KEY_F = Key.F; +export const KEY_G = Key.G; +export const KEY_H = Key.H; +export const KEY_I = Key.I; +export const KEY_J = Key.J; +export const KEY_K = Key.K; +export const KEY_L = Key.L; +export const KEY_M = Key.M; +export const KEY_N = Key.N; +export const KEY_O = Key.O; +export const KEY_P = Key.P; +export const KEY_Q = Key.Q; +export const KEY_R = Key.R; +export const KEY_S = Key.S; +export const KEY_T = Key.T; +export const KEY_U = Key.U; +export const KEY_V = Key.V; +export const KEY_W = Key.W; +export const KEY_X = Key.X; +export const KEY_Y = Key.Y; +export const KEY_Z = Key.Z; +export const KEY_WINDOWS = Key.Windows; +export const KEY_CONTEXT_MENU = Key.ContextMenu; +export const KEY_NUMPAD_0 = Key.Numpad0; +export const KEY_NUMPAD_1 = Key.Numpad1; +export const KEY_NUMPAD_2 = Key.Numpad2; +export const KEY_NUMPAD_3 = Key.Numpad3; +export const KEY_NUMPAD_4 = Key.Numpad4; +export const KEY_NUMPAD_5 = Key.Numpad5; +export const KEY_NUMPAD_6 = Key.Numpad6; +export const KEY_NUMPAD_7 = Key.Numpad7; +export const KEY_NUMPAD_8 = Key.Numpad8; +export const KEY_NUMPAD_9 = Key.Numpad9; +export const KEY_MULTIPLY = Key.Multiply; +export const KEY_ADD = Key.Add; +export const KEY_SEPARATOR = Key.Separator; +export const KEY_SUBTRACT = Key.Subtract; +export const KEY_DECIMAL = Key.Decimal; +export const KEY_DIVIDE = Key.Divide; +export const KEY_F1 = Key.F1; +export const KEY_F2 = Key.F2; +export const KEY_F3 = Key.F3; +export const KEY_F4 = Key.F4; +export const KEY_F5 = Key.F5; +export const KEY_F6 = Key.F6; +export const KEY_F7 = Key.F7; +export const KEY_F8 = Key.F8; +export const KEY_F9 = Key.F9; +export const KEY_F10 = Key.F10; +export const KEY_F11 = Key.F11; +export const KEY_F12 = Key.F12; +export const KEY_COMMA = Key.Comma; +export const KEY_PERIOD = Key.Period; +export const KEY_SLASH = Key.Slash; +export const KEY_OPEN_BRACKET = Key.OpenBracket; +export const KEY_BACK_SLASH = Key.BackSlash; +export const KEY_CLOSE_BRACKET = Key.CloseBracket; +export const KEY_META = Key.Meta; + export const MOUSEBUTTON_NONE = MouseButton.None; export const MOUSEBUTTON_LEFT = MouseButton.Left; export const MOUSEBUTTON_MIDDLE = MouseButton.Middle; diff --git a/src/framework/app-base.js b/src/framework/app-base.js index 3145d869e28..47b91fadde9 100644 --- a/src/framework/app-base.js +++ b/src/framework/app-base.js @@ -284,7 +284,7 @@ class AppBase extends EventHandler { * @type {boolean} * @example * // Render the scene only while space key is pressed - * if (this.app.keyboard.isPressed(pc.KEY_SPACE)) { + * if (this.app.keyboard.isPressed(pc.Key.Space)) { * this.app.renderNextFrame = true; * } */ diff --git a/src/framework/xr/xr-manager.js b/src/framework/xr/xr-manager.js index 509d6c1f33c..504b3437194 100644 --- a/src/framework/xr/xr-manager.js +++ b/src/framework/xr/xr-manager.js @@ -569,7 +569,7 @@ class XrManager extends EventHandler { * session. * @example * app.keyboard.on('keydown', (evt) => { - * if (evt.key === pc.KEY_ESCAPE && app.xr.active) { + * if (evt.key === pc.Key.Escape && app.xr.active) { * app.xr.end(); * } * }); diff --git a/src/platform/input/constants.js b/src/platform/input/constants.js index 84e7db5a119..d50bde528b1 100644 --- a/src/platform/input/constants.js +++ b/src/platform/input/constants.js @@ -10,588 +10,6 @@ export const AXIS_PAD_R_X = 'padrx'; export const AXIS_PAD_R_Y = 'padry'; export const AXIS_KEY = 'key'; -/** - * @type {number} - * @category Input - */ -export const KEY_BACKSPACE = 8; - -/** - * @type {number} - * @category Input - */ -export const KEY_TAB = 9; - -/** - * @type {number} - * @category Input - */ -export const KEY_RETURN = 13; - -/** - * @type {number} - * @category Input - */ -export const KEY_ENTER = 13; - -/** - * @type {number} - * @category Input - */ -export const KEY_SHIFT = 16; - -/** - * @type {number} - * @category Input - */ -export const KEY_CONTROL = 17; - -/** - * @type {number} - * @category Input - */ -export const KEY_ALT = 18; - -/** - * @type {number} - * @category Input - */ -export const KEY_PAUSE = 19; - -/** - * @type {number} - * @category Input - */ -export const KEY_CAPS_LOCK = 20; - -/** - * @type {number} - * @category Input - */ -export const KEY_ESCAPE = 27; - -/** - * @type {number} - * @category Input - */ -export const KEY_SPACE = 32; - -/** - * @type {number} - * @category Input - */ -export const KEY_PAGE_UP = 33; - -/** - * @type {number} - * @category Input - */ -export const KEY_PAGE_DOWN = 34; - -/** - * @type {number} - * @category Input - */ -export const KEY_END = 35; - -/** - * @type {number} - * @category Input - */ -export const KEY_HOME = 36; - -/** - * @type {number} - * @category Input - */ -export const KEY_LEFT = 37; - -/** - * @type {number} - * @category Input - */ -export const KEY_UP = 38; - -/** - * @type {number} - * @category Input - */ -export const KEY_RIGHT = 39; - -/** - * @type {number} - * @category Input - */ -export const KEY_DOWN = 40; - -/** - * @type {number} - * @category Input - */ -export const KEY_PRINT_SCREEN = 44; - -/** - * @type {number} - * @category Input - */ -export const KEY_INSERT = 45; - -/** - * @type {number} - * @category Input - */ -export const KEY_DELETE = 46; - -/** - * @type {number} - * @category Input - */ -export const KEY_0 = 48; - -/** - * @type {number} - * @category Input - */ -export const KEY_1 = 49; - -/** - * @type {number} - * @category Input - */ -export const KEY_2 = 50; - -/** - * @type {number} - * @category Input - */ -export const KEY_3 = 51; - -/** - * @type {number} - * @category Input - */ -export const KEY_4 = 52; - -/** - * @type {number} - * @category Input - */ -export const KEY_5 = 53; - -/** - * @type {number} - * @category Input - */ -export const KEY_6 = 54; - -/** - * @type {number} - * @category Input - */ -export const KEY_7 = 55; - -/** - * @type {number} - * @category Input - */ -export const KEY_8 = 56; - -/** - * @type {number} - * @category Input - */ -export const KEY_9 = 57; - -/** - * @type {number} - * @category Input - */ -export const KEY_SEMICOLON = 59; - -/** - * @type {number} - * @category Input - */ -export const KEY_EQUAL = 61; - -/** - * @type {number} - * @category Input - */ -export const KEY_A = 65; - -/** - * @type {number} - * @category Input - */ -export const KEY_B = 66; - -/** - * @type {number} - * @category Input - */ -export const KEY_C = 67; - -/** - * @type {number} - * @category Input - */ -export const KEY_D = 68; - -/** - * @type {number} - * @category Input - */ -export const KEY_E = 69; - -/** - * @type {number} - * @category Input - */ -export const KEY_F = 70; - -/** - * @type {number} - * @category Input - */ -export const KEY_G = 71; - -/** - * @type {number} - * @category Input - */ -export const KEY_H = 72; - -/** - * @type {number} - * @category Input - */ -export const KEY_I = 73; - -/** - * @type {number} - * @category Input - */ -export const KEY_J = 74; - -/** - * @type {number} - * @category Input - */ -export const KEY_K = 75; - -/** - * @type {number} - * @category Input - */ -export const KEY_L = 76; - -/** - * @type {number} - * @category Input - */ -export const KEY_M = 77; - -/** - * @type {number} - * @category Input - */ -export const KEY_N = 78; - -/** - * @type {number} - * @category Input - */ -export const KEY_O = 79; - -/** - * @type {number} - * @category Input - */ -export const KEY_P = 80; - -/** - * @type {number} - * @category Input - */ -export const KEY_Q = 81; - -/** - * @type {number} - * @category Input - */ -export const KEY_R = 82; - -/** - * @type {number} - * @category Input - */ -export const KEY_S = 83; - -/** - * @type {number} - * @category Input - */ -export const KEY_T = 84; - -/** - * @type {number} - * @category Input - */ -export const KEY_U = 85; - -/** - * @type {number} - * @category Input - */ -export const KEY_V = 86; - -/** - * @type {number} - * @category Input - */ -export const KEY_W = 87; - -/** - * @type {number} - * @category Input - */ -export const KEY_X = 88; - -/** - * @type {number} - * @category Input - */ -export const KEY_Y = 89; - -/** - * @type {number} - * @category Input - */ -export const KEY_Z = 90; - -/** - * @type {number} - * @category Input - */ -export const KEY_WINDOWS = 91; - -/** - * @type {number} - * @category Input - */ -export const KEY_CONTEXT_MENU = 93; - -/** - * @type {number} - * @category Input - */ -export const KEY_NUMPAD_0 = 96; - -/** - * @type {number} - * @category Input - */ -export const KEY_NUMPAD_1 = 97; - -/** - * @type {number} - * @category Input - */ -export const KEY_NUMPAD_2 = 98; - -/** - * @type {number} - * @category Input - */ -export const KEY_NUMPAD_3 = 99; - -/** - * @type {number} - * @category Input - */ -export const KEY_NUMPAD_4 = 100; - -/** - * @type {number} - * @category Input - */ -export const KEY_NUMPAD_5 = 101; - -/** - * @type {number} - * @category Input - */ -export const KEY_NUMPAD_6 = 102; - -/** - * @type {number} - * @category Input - */ -export const KEY_NUMPAD_7 = 103; - -/** - * @type {number} - * @category Input - */ -export const KEY_NUMPAD_8 = 104; - -/** - * @type {number} - * @category Input - */ -export const KEY_NUMPAD_9 = 105; - -/** - * @type {number} - * @category Input - */ -export const KEY_MULTIPLY = 106; - -/** - * @type {number} - * @category Input - */ -export const KEY_ADD = 107; - -/** - * @type {number} - * @category Input - */ -export const KEY_SEPARATOR = 108; - -/** - * @type {number} - * @category Input - */ -export const KEY_SUBTRACT = 109; - -/** - * @type {number} - * @category Input - */ -export const KEY_DECIMAL = 110; - -/** - * @type {number} - * @category Input - */ -export const KEY_DIVIDE = 111; - -/** - * @type {number} - * @category Input - */ -export const KEY_F1 = 112; - -/** - * @type {number} - * @category Input - */ -export const KEY_F2 = 113; - -/** - * @type {number} - * @category Input - */ -export const KEY_F3 = 114; - -/** - * @type {number} - * @category Input - */ -export const KEY_F4 = 115; - -/** - * @type {number} - * @category Input - */ -export const KEY_F5 = 116; - -/** - * @type {number} - * @category Input - */ -export const KEY_F6 = 117; - -/** - * @type {number} - * @category Input - */ -export const KEY_F7 = 118; - -/** - * @type {number} - * @category Input - */ -export const KEY_F8 = 119; - -/** - * @type {number} - * @category Input - */ -export const KEY_F9 = 120; - -/** - * @type {number} - * @category Input - */ -export const KEY_F10 = 121; - -/** - * @type {number} - * @category Input - */ -export const KEY_F11 = 122; - -/** - * @type {number} - * @category Input - */ -export const KEY_F12 = 123; - -/** - * @type {number} - * @category Input - */ -export const KEY_COMMA = 188; - -/** - * @type {number} - * @category Input - */ -export const KEY_PERIOD = 190; - -/** - * @type {number} - * @category Input - */ -export const KEY_SLASH = 191; - -/** - * @type {number} - * @category Input - */ -export const KEY_OPEN_BRACKET = 219; - -/** - * @type {number} - * @category Input - */ -export const KEY_BACK_SLASH = 220; - -/** - * @type {number} - * @category Input - */ -export const KEY_CLOSE_BRACKET = 221; - -/** - * @type {number} - * @category Input - */ -export const KEY_META = 224; - /** * Index for pad 1. * diff --git a/src/platform/input/controller.js b/src/platform/input/controller.js index bfe36a9b0cb..8c11362c2fa 100644 --- a/src/platform/input/controller.js +++ b/src/platform/input/controller.js @@ -62,7 +62,7 @@ class Controller { * const c = new pc.Controller(document); * * // Register the "fire" action and assign it to both the Enter key and the space bar. - * c.registerKeys("fire", [pc.KEY_ENTER, pc.KEY_SPACE]); + * c.registerKeys("fire", [pc.Key.Enter, pc.Key.Space]); */ constructor(element, options = {}) { this._keyboard = options.keyboard || null; @@ -158,8 +158,8 @@ class Controller { * @param {string} action_name - The name of the action. * @param {object} action - An action object to add. * @param {ACTION_KEYBOARD | ACTION_MOUSE | ACTION_GAMEPAD} action.type - The name of the action. - * @param {number[]} [action.keys] - Keyboard: A list of keycodes e.g. `[pc.KEY_A, pc.KEY_ENTER]`. - * @param {number} [action.button] - Mouse: e.g. `pc.MouseButton.LEFT` - Gamepad: e.g. `pc.PAD_FACE_1` + * @param {number[]} [action.keys] - Keyboard: A list of keycodes e.g. `[pc.Key.A, pc.Key.Enter]`. + * @param {number} [action.button] - Mouse: e.g. `pc.MouseButton.Left` - Gamepad: e.g. `pc.PAD_FACE_1` * @param {number} [action.pad] - Gamepad: An index of the pad to register (use {@link PAD_1}, etc). */ appendAction(action_name, action) { diff --git a/src/platform/input/keyboard-event.js b/src/platform/input/keyboard-event.js index 11a1a69bb33..f9fcdce5bc8 100644 --- a/src/platform/input/keyboard-event.js +++ b/src/platform/input/keyboard-event.js @@ -1,4 +1,5 @@ /** + * @import { Key } from './key.js'; * @import { Keyboard } from './keyboard.js' */ @@ -13,9 +14,9 @@ */ class KeyboardEvent { /** - * The keyCode of the key that has changed. See the KEY_* constants. + * The keyCode of the key that has changed. * - * @type {number|null} + * @type {Key|null} */ key = null; @@ -40,7 +41,7 @@ class KeyboardEvent { * @param {globalThis.KeyboardEvent} event - The original browser event that was fired. * @example * const onKeyDown = function (e) { - * if (e.key === pc.KEY_SPACE) { + * if (e.key === pc.Key.Space) { * // space key pressed * } * e.event.preventDefault(); // Use original browser event to prevent browser action. diff --git a/src/platform/input/keyboard.js b/src/platform/input/keyboard.js index 34f1bfc8f88..74604f50291 100644 --- a/src/platform/input/keyboard.js +++ b/src/platform/input/keyboard.js @@ -3,6 +3,10 @@ import { EventHandler } from '../../core/event-handler.js'; import { KeyboardEvent } from './keyboard-event.js'; +/** + * @import { Key } from './key.js' + */ + // internal global keyboard events const _keyboardEvent = new KeyboardEvent(); @@ -70,7 +74,7 @@ class Keyboard extends EventHandler { * @event * @example * const onKeyDown = (e) => { - * if (e.key === pc.KEY_SPACE) { + * if (e.key === pc.Key.Space) { * // space key pressed * } * e.event.preventDefault(); // Use original browser event to prevent browser action. @@ -86,7 +90,7 @@ class Keyboard extends EventHandler { * @event * @example * const onKeyUp = (e) => { - * if (e.key === pc.KEY_SPACE) { + * if (e.key === pc.Key.Space) { * // space key released * } * e.event.preventDefault(); // Use original browser event to prevent browser action. @@ -313,7 +317,7 @@ class Keyboard extends EventHandler { /** * Return true if the key is currently down. * - * @param {number} key - The keyCode of the key to test. See the KEY_* constants. + * @param {Key} key - The keyCode of the key to test. * @returns {boolean} True if the key was pressed, false if not. */ isPressed(key) { @@ -326,7 +330,7 @@ class Keyboard extends EventHandler { /** * Returns true if the key was pressed since the last update. * - * @param {number} key - The keyCode of the key to test. See the KEY_* constants. + * @param {Key} key - The keyCode of the key to test. * @returns {boolean} True if the key was pressed. */ wasPressed(key) { @@ -339,7 +343,7 @@ class Keyboard extends EventHandler { /** * Returns true if the key was released since the last update. * - * @param {number} key - The keyCode of the key to test. See the KEY_* constants. + * @param {Key} key - The keyCode of the key to test. * @returns {boolean} True if the key was pressed. */ wasReleased(key) {