@hydroperx/inputaction
Advanced tools
+0
-8
| import Input from "./Input"; | ||
| import { InputActionAtom, InputActionKeyName } from "./InputAction"; | ||
| export { Input }; | ||
| export type { InputActionAtom, InputActionKey, InputActionKeyName, } from "./InputAction"; | ||
| /** | ||
| * Returns the display text of a shortcut, such as `"Ctrl+A"`. | ||
| * | ||
| * @param param Either an action name or a series of action atoms. | ||
| */ | ||
| export declare function shortcutDisplayText(param: string | InputActionAtom[]): string; | ||
| export declare function inputActionKeyNameDisplayText(name: InputActionKeyName): string; |
+0
-67
| import Input from "./Input"; | ||
| export { Input }; | ||
| /** | ||
| * Returns the display text of a shortcut, such as `"Ctrl+A"`. | ||
| * | ||
| * @param param Either an action name or a series of action atoms. | ||
| */ | ||
| export function shortcutDisplayText(param) { | ||
| if (typeof param == "string") { | ||
| return shortcutDisplayText(Input.input.getActions()[param]); | ||
| } | ||
| if (!param) | ||
| return ""; | ||
| for (const atom of param) { | ||
| if (atom.hasOwnProperty("key")) { | ||
| const key = atom; | ||
| const parts = []; | ||
| if (key.control) { | ||
| parts.push("Ctrl"); | ||
| } | ||
| if (key.alt) { | ||
| parts.push("Alt"); | ||
| } | ||
| if (key.shift) { | ||
| parts.push("Shift"); | ||
| } | ||
| parts.push(inputActionKeyNameDisplayText(key.key)); | ||
| return parts.join("+"); | ||
| } | ||
| } | ||
| return ""; | ||
| } | ||
| export function inputActionKeyNameDisplayText(name) { | ||
| switch (name) { | ||
| case "leftArrow": return "Left"; | ||
| case "rightArrow": return "Right"; | ||
| case "upArrow": return "Up"; | ||
| case "downArrow": return "Down"; | ||
| case "spacebar": return "Space"; | ||
| case "enter": return "Enter"; | ||
| case "backspace": return "Backspace"; | ||
| case "minus": return "Minus"; | ||
| case "plus": return "Plus"; | ||
| case "tab": return "Tab"; | ||
| case "f1": return "F1"; | ||
| case "f2": return "F2"; | ||
| case "f3": return "F3"; | ||
| case "f4": return "F4"; | ||
| case "f5": return "F5"; | ||
| case "f6": return "F6"; | ||
| case "f7": return "F7"; | ||
| case "f8": return "F8"; | ||
| case "f9": return "F9"; | ||
| case "f10": return "F10"; | ||
| case "f11": return "F11"; | ||
| case "f12": return "F12"; | ||
| case "0": return "0"; | ||
| case "1": return "1"; | ||
| case "2": return "2"; | ||
| case "3": return "3"; | ||
| case "4": return "4"; | ||
| case "5": return "5"; | ||
| case "6": return "6"; | ||
| case "7": return "7"; | ||
| case "8": return "8"; | ||
| case "9": return "9"; | ||
| default: return name.toUpperCase(); | ||
| } | ||
| } |
+11
-1
@@ -1,2 +0,2 @@ | ||
| import { InputActionAtom } from "./InputAction"; | ||
| import { InputActionAtom, InputActionKeyName } from "./InputAction"; | ||
| import { TypedEventTarget } from "@hydroperx/event"; | ||
@@ -73,2 +73,12 @@ declare const Input_base: TypedEventTarget<{ | ||
| static readonly input: Input; | ||
| /** | ||
| * Returns the display text of a shortcut, such as `"Ctrl+A"`. | ||
| * | ||
| * @param param Either an action name or a series of action atoms. | ||
| */ | ||
| static display(param: string | InputActionAtom[]): string; | ||
| /** | ||
| * Returns the display text of an individual key name. | ||
| */ | ||
| static keyNameDisplay(name: InputActionKeyName): string; | ||
| private mMap; | ||
@@ -75,0 +85,0 @@ /** |
+94
-20
@@ -1,3 +0,2 @@ | ||
| import clonePlainObject from "./util/clonePlainObject"; | ||
| import { navigatorKeyToThis } from "./InputAction"; | ||
| import { navigatorKeyToThis, } from "./InputAction"; | ||
| import assert from "assert"; | ||
@@ -68,3 +67,72 @@ /** | ||
| */ | ||
| static input = new Input; | ||
| static input = new Input(); | ||
| /** | ||
| * Returns the display text of a shortcut, such as `"Ctrl+A"`. | ||
| * | ||
| * @param param Either an action name or a series of action atoms. | ||
| */ | ||
| static display(param) { | ||
| if (typeof param == "string") { | ||
| return Input.display(Input.input.getActions()[param]); | ||
| } | ||
| if (!param) | ||
| return ""; | ||
| for (const atom of param) { | ||
| if (atom.hasOwnProperty("key")) { | ||
| const key = atom; | ||
| const parts = []; | ||
| if (key.control) { | ||
| parts.push("Ctrl"); | ||
| } | ||
| if (key.alt) { | ||
| parts.push("Alt"); | ||
| } | ||
| if (key.shift) { | ||
| parts.push("Shift"); | ||
| } | ||
| parts.push(Input.keyNameDisplay(key.key)); | ||
| return parts.join("+"); | ||
| } | ||
| } | ||
| return ""; | ||
| } | ||
| /** | ||
| * Returns the display text of an individual key name. | ||
| */ | ||
| static keyNameDisplay(name) { | ||
| switch (name) { | ||
| case "escape": | ||
| return "Esc"; | ||
| case "leftArrow": | ||
| return "Left"; | ||
| case "rightArrow": | ||
| return "Right"; | ||
| case "upArrow": | ||
| return "Up"; | ||
| case "downArrow": | ||
| return "Down"; | ||
| case "spacebar": | ||
| return "Space"; | ||
| case "enter": | ||
| return "Enter"; | ||
| case "backspace": | ||
| return "Backspace"; | ||
| case "minus": | ||
| return "Minus"; | ||
| case "plus": | ||
| return "Plus"; | ||
| case "tab": | ||
| return "Tab"; | ||
| case "assign": | ||
| return "="; | ||
| case "comma": | ||
| return ","; | ||
| case "dot": | ||
| return "."; | ||
| case "semicolon": | ||
| return ";"; | ||
| default: | ||
| return name.toUpperCase(); | ||
| } | ||
| } | ||
| // Actions map | ||
@@ -78,3 +146,3 @@ mMap = { | ||
| getActions() { | ||
| return clonePlainObject(this.mMap, true); | ||
| return structuredClone(this.mMap); | ||
| } | ||
@@ -89,3 +157,3 @@ /** | ||
| ...Input.builtin(), | ||
| ...clonePlainObject(map, true), | ||
| ...structuredClone(map), | ||
| }; | ||
@@ -97,7 +165,7 @@ // Dispatch update event | ||
| return { | ||
| "escape": [{ key: "escape" }], | ||
| "navigateLeft": [{ key: "leftArrow" }], | ||
| "navigateRight": [{ key: "rightArrow" }], | ||
| "navigateUp": [{ key: "upArrow" }], | ||
| "navigateDown": [{ key: "downArrow" }], | ||
| escape: [{ key: "escape" }], | ||
| navigateLeft: [{ key: "leftArrow" }], | ||
| navigateRight: [{ key: "rightArrow" }], | ||
| navigateUp: [{ key: "upArrow" }], | ||
| navigateDown: [{ key: "downArrow" }], | ||
| }; | ||
@@ -109,3 +177,3 @@ } | ||
| if (typeof window !== "undefined") { | ||
| window.addEventListener("keydown", evt => { | ||
| window.addEventListener("keydown", (evt) => { | ||
| const keyName = navigatorKeyToThis(evt.key); | ||
@@ -139,3 +207,3 @@ if (keyName !== undefined) { | ||
| }); | ||
| window.addEventListener("keyup", evt => { | ||
| window.addEventListener("keyup", (evt) => { | ||
| const keyName = navigatorKeyToThis(evt.key); | ||
@@ -181,6 +249,9 @@ if (keyName !== undefined) { | ||
| const pressedState = Input.mPressedStatePoolKeys.get(inputActionKey.key); | ||
| const pressed = pressedState !== undefined && pressedState.pressed | ||
| && (inputActionKey.control ? pressedState.control : !pressedState.control) | ||
| && (inputActionKey.shift ? pressedState.shift : !pressedState.shift) | ||
| && (inputActionKey.alt ? pressedState.alt : !pressedState.alt); | ||
| const pressed = pressedState !== undefined && | ||
| pressedState.pressed && | ||
| (inputActionKey.control | ||
| ? pressedState.control | ||
| : !pressedState.control) && | ||
| (inputActionKey.shift ? pressedState.shift : !pressedState.shift) && | ||
| (inputActionKey.alt ? pressedState.alt : !pressedState.alt); | ||
| if (pressed) { | ||
@@ -204,6 +275,9 @@ return true; | ||
| const pressedState = Input.mPressedStatePoolKeys.get(inputActionKey.key); | ||
| const pressed = pressedState !== undefined && pressedState.pressed | ||
| && (inputActionKey.control ? pressedState.control : !pressedState.control) | ||
| && (inputActionKey.shift ? pressedState.shift : !pressedState.shift) | ||
| && (inputActionKey.alt ? pressedState.alt : !pressedState.alt); | ||
| const pressed = pressedState !== undefined && | ||
| pressedState.pressed && | ||
| (inputActionKey.control | ||
| ? pressedState.control | ||
| : !pressedState.control) && | ||
| (inputActionKey.shift ? pressedState.shift : !pressedState.shift) && | ||
| (inputActionKey.alt ? pressedState.alt : !pressedState.alt); | ||
| if (pressed && pressedState.pressedTimestamp > Date.now() - 15) { | ||
@@ -210,0 +284,0 @@ return true; |
+1
-1
| { | ||
| "name": "@hydroperx/inputaction", | ||
| "version": "1.0.2", | ||
| "version": "1.0.3", | ||
| "description": "Input actions library.", | ||
@@ -5,0 +5,0 @@ "repository": "https://github.com/hydroperx/inputaction", |
28786
0.05%488
1.88%