umbr-key-master 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # Key master
2
+
3
+ A simple vanilla JS libary to handle key events and run logic
@@ -0,0 +1,46 @@
1
+ import { Alphabet, AudioKey, ControlCharacter, DeviceKey, EditingKey, FunctionKey, GlyphModifierKey, IMEKey, ModifierKey, MultimediaKey, MultimediaNumpadKey, NavigationKey, SpecialKey, SpeechKey, UIKey, WhitespaceKey } from "./w3";
2
+ /**
3
+ * Helper used to get custom data to pass to your callback when its conditions are met
4
+ */
5
+ type GetDataCallback = (() => any | Promise<any>) | null;
6
+ /**
7
+ * The key value of a keydown can be mapped from W3C UI Events / DOM spec
8
+ *
9
+ * these are what a keydown / keyup `key` value can be and is what you can pass to listen to specific combinations to trigger logic
10
+ */
11
+ type PressableKey = Alphabet | ControlCharacter | GlyphModifierKey | SpecialKey | ModifierKey | WhitespaceKey | NavigationKey | EditingKey | UIKey | DeviceKey | IMEKey | FunctionKey | MultimediaKey | MultimediaNumpadKey | AudioKey | SpeechKey;
12
+ /**
13
+ * Custom callback to run when the specific keys registered are met
14
+ */
15
+ type KeyCallback = (...args: any[]) => any | Promise<any>;
16
+ /**
17
+ * Represents a key combination manager that holds callbacks to run for specific key combinations
18
+ */
19
+ export declare class KeyMaster {
20
+ private _getData;
21
+ /**
22
+ * Holds a list of current keys being pressed down
23
+ */
24
+ private readonly _keys;
25
+ /**
26
+ * Contains all the callbacks to run
27
+ */
28
+ private readonly _callbacks;
29
+ constructor(getDataFunc?: GetDataCallback);
30
+ /**
31
+ * Creates a key for list of pressable keys
32
+ * @param keys The list of keys to make an ID for specific callbacks
33
+ * @returns Key
34
+ */
35
+ private makeKey;
36
+ private handleKeyDown;
37
+ private run;
38
+ private handleKeyUp;
39
+ add(targetKeys: PressableKey[], callback: KeyCallback): void;
40
+ remove(callback: KeyCallback): void;
41
+ /**
42
+ * Call before destroying to clean up event listeners
43
+ */
44
+ dispose(): void;
45
+ }
46
+ export {};
package/dist/index.js ADDED
@@ -0,0 +1,84 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ /**
11
+ * Represents a key combination manager that holds callbacks to run for specific key combinations
12
+ */
13
+ export class KeyMaster {
14
+ constructor(getDataFunc = null) {
15
+ this._getData = null;
16
+ /**
17
+ * Holds a list of current keys being pressed down
18
+ */
19
+ this._keys = new Set();
20
+ /**
21
+ * Contains all the callbacks to run
22
+ */
23
+ this._callbacks = new Map();
24
+ this.handleKeyDown = (event) => {
25
+ this._keys.add(event.key);
26
+ const pressed = Array.from(this._keys.values());
27
+ const key = this.makeKey(pressed);
28
+ const callbacks = this._callbacks.get(key);
29
+ if (callbacks) {
30
+ this.run(callbacks);
31
+ }
32
+ };
33
+ this.handleKeyUp = (event) => {
34
+ this._keys.delete(event.key);
35
+ };
36
+ this._getData = getDataFunc;
37
+ document.addEventListener("keydown", this.handleKeyDown);
38
+ document.addEventListener("keyup", this.handleKeyUp);
39
+ }
40
+ /**
41
+ * Creates a key for list of pressable keys
42
+ * @param keys The list of keys to make an ID for specific callbacks
43
+ * @returns Key
44
+ */
45
+ makeKey(keys) {
46
+ return keys.sort().join("_");
47
+ }
48
+ run(callbacks) {
49
+ return __awaiter(this, void 0, void 0, function* () {
50
+ const data = this._getData ? yield this._getData() : null;
51
+ for (const callback of callbacks) {
52
+ yield callback(data);
53
+ }
54
+ });
55
+ }
56
+ add(targetKeys, callback) {
57
+ const key = this.makeKey(targetKeys);
58
+ if (!this._callbacks.has(key)) {
59
+ this._callbacks.set(key, []);
60
+ }
61
+ this._callbacks.get(key).push(callback);
62
+ }
63
+ remove(callback) {
64
+ for (const [key, callbacks] of this._callbacks.entries()) {
65
+ const index = callbacks.indexOf(callback);
66
+ if (index !== -1) {
67
+ callbacks.splice(index, 1);
68
+ if (callbacks.length === 0) {
69
+ this._callbacks.delete(key);
70
+ }
71
+ }
72
+ }
73
+ }
74
+ /**
75
+ * Call before destroying to clean up event listeners
76
+ */
77
+ dispose() {
78
+ document.removeEventListener("keydown", this.handleKeyDown);
79
+ document.removeEventListener("keyup", this.handleKeyUp);
80
+ this._callbacks.clear();
81
+ this._keys.clear();
82
+ }
83
+ }
84
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;AAoDA;;GAEG;AACH,MAAM,OAAO,SAAS;IAapB,YAAY,cAA+B,IAAI;QAZvC,aAAQ,GAAoB,IAAI,CAAC;QAEzC;;WAEG;QACc,UAAK,GAAG,IAAI,GAAG,EAAgB,CAAC;QAEjD;;WAEG;QACc,eAAU,GAA+B,IAAI,GAAG,EAAE,CAAC;QAkB5D,kBAAa,GAAG,CAAC,KAAoB,EAAE,EAAE;YAC/C,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,GAAmB,CAAC,CAAC;YAE1C,MAAM,OAAO,GAAG,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC;YAChD,MAAM,GAAG,GAAG,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;YAElC,MAAM,SAAS,GAAG,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;YAC3C,IAAI,SAAS,EAAE,CAAC;gBACd,IAAI,CAAC,GAAG,CAAC,SAAS,CA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IAAI,CAAC,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC;YACzD,MAAM,KAAK,GAAG,SAAS,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;YAC1C,IAAI,KAAK,KAAK,CAAC,CAAC,EAAE,CAAC;gBACjB,SAAS,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;gBAC3B,IAAI,SAAS,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;oBAC3B,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;gBAC9B,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;IAED;;OAEG;IACH,OAAO;QACL,QAAQ,CAAC,mBAAmB,CAAC,SAAS,EAAE,IAAI,CAAC,aAAa,CAAC,CAAC;QAC5D,QAAQ,CAAC,mBAAmB,CAAC,OAAO,EAAE,IAAI,CAAC,WAAW,CAAC,CAAC;QACxD,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,CAAC;QACxB,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC;IACrB,CAAC;CACF"}
package/dist/w3.d.ts ADDED
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Mapped from https://www.w3.org/TR/uievents-key/#key-attr-values (only english set) for all values a keydown / keyup `key` can be mapped into
3
+ * TS types for better intelisense
4
+ */
5
+ /**
6
+ * Contains all english alphabet charcters inclduing uppercase
7
+ */
8
+ export type Alphabet = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z" | "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z";
9
+ export type ControlCharacter = "Backspace" | "Tab" | "Enter" | "Escape" | "Delete";
10
+ export type GlyphModifierKey = "Shift" | "CapsLock" | "AltGr";
11
+ export type SpecialKey = "Unidentified";
12
+ export type ModifierKey = "Alt" | "AltGraph" | "CapsLock" | "Control" | "Fn" | "FnLock" | "Meta" | "NumLock" | "ScrollLock" | "Shift" | "Symbol" | "SymbolLock" | "Hyper" | "Super";
13
+ export type WhitespaceKey = "Enter" | "Tab";
14
+ export type NavigationKey = "ArrowDown" | "ArrowLeft" | "ArrowRight" | "ArrowUp" | "End" | "Home" | "GoHome" | "PageDown" | "PageUp";
15
+ export type EditingKey = "Backspace" | "Clear" | "Copy" | "CrSel" | "Cut" | "Delete" | "EraseEof" | "ExSel" | "Insert" | "Paste" | "Redo" | "Undo";
16
+ export type UIKey = "Accept" | "Again" | "Attn" | "Cancel" | "ContextMenu" | "Escape" | "Execute" | "Find" | "Help" | "Pause" | "Play" | "Props" | "Select" | "ZoomIn" | "ZoomOut";
17
+ export type DeviceKey = "BrightnessDown" | "BrightnessUp" | "Eject" | "LogOff" | "Power" | "PowerOff" | "PrintScreen" | "Hibernate" | "Standby" | "WakeUp";
18
+ export type IMEKey = "AllCandidates" | "Alphanumeric" | "CodeInput" | "Compose" | "Convert" | "Dead" | "FinalMode" | "GroupFirst" | "GroupLast" | "GroupNext" | "GroupPrevious" | "ModeChange" | "NextCandidate" | "NonConvert" | "PreviousCandidate" | "Process" | "SingleCandidate" | "HangulMode" | "HanjaMode" | "JunjaMode" | "Eisu" | "Hankaku" | "Hiragana" | "HiraganaKatakana" | "KanaMode" | "KanjiMode" | "Katakana" | "Romaji" | "Zenkaku" | "ZenkakuHankaku";
19
+ export type FunctionKey = "F1" | "F2" | "F3" | "F4" | "F5" | "F6" | "F7" | "F8" | "F9" | "F10" | "F11" | "F12" | "Soft1" | "Soft2" | "Soft3" | "Soft4";
20
+ export type MultimediaKey = "ChannelDown" | "ChannelUp" | "Close" | "MailForward" | "MailReply" | "MailSend" | "MediaClose" | "MediaFastForward" | "MediaPause" | "MediaPlay" | "MediaPlayPause" | "MediaRecord" | "MediaRewind" | "MediaStop" | "MediaTrackNext" | "MediaTrackPrevious" | "New" | "Open" | "Print" | "Save" | "SpellCheck";
21
+ export type MultimediaNumpadKey = "Key11" | "Key12";
22
+ export type AudioKey = "AudioBalanceLeft" | "AudioBalanceRight" | "AudioBassBoostDown" | "AudioBassBoostToggle" | "AudioBassBoostUp" | "AudioFaderFront" | "AudioFaderRear" | "AudioSurroundModeNext" | "AudioTrebleDown" | "AudioTrebleUp" | "AudioVolumeDown" | "AudioVolumeUp" | "AudioVolumeMute" | "MicrophoneToggle" | "MicrophoneVolumeDown" | "MicrophoneVolumeUp" | "MicrophoneVolumeMute";
23
+ export type SpeechKey = "SpeechCorrectionList" | "SpeechInputToggle";
package/dist/w3.js ADDED
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Mapped from https://www.w3.org/TR/uievents-key/#key-attr-values (only english set) for all values a keydown / keyup `key` can be mapped into
3
+ * TS types for better intelisense
4
+ */
5
+ export {};
6
+ //# sourceMappingURL=w3.js.map
package/dist/w3.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"w3.js","sourceRoot":"","sources":["../src/w3.ts"],"names":[],"mappings":"AAAA;;;GAGG"}
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "umbr-key-master",
3
+ "version": "1.0.0",
4
+ "description": "A simple key combination helper",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "scripts": {
8
+ "build": "tsc"
9
+ },
10
+ "keywords": [
11
+ "key",
12
+ "keyboard",
13
+ "shortcut",
14
+ "combination"
15
+ ],
16
+ "author": "Yousaf Wazir",
17
+ "license": "ISC",
18
+ "files": [
19
+ "dist",
20
+ "README.md"
21
+ ],
22
+ "devDependencies": {
23
+ "typescript": "^5.9.3"
24
+ }
25
+ }