@leonabcd123/modern-caps-lock 3.1.3 → 3.1.5
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 +1 -1
- package/lib/index.d.ts +3 -3
- package/lib/index.js +37 -26
- package/package.json +1 -1
package/README.md
CHANGED
package/lib/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
type OnCapsChangeCallback = (capsState: boolean) => void;
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export {};
|
|
2
|
+
declare function isCapsLockOn(): boolean;
|
|
3
|
+
declare function onCapsLockChange(callback: OnCapsChangeCallback): void;
|
|
4
|
+
export { isCapsLockOn, onCapsLockChange };
|
package/lib/index.js
CHANGED
|
@@ -1,17 +1,16 @@
|
|
|
1
1
|
var _a, _b;
|
|
2
2
|
import { getCurrentOs } from "./os-detection.js";
|
|
3
|
-
let previousCapsState = false;
|
|
4
3
|
let capsState = false;
|
|
5
4
|
const os = getCurrentOs();
|
|
6
|
-
|
|
5
|
+
const onCapsChangeCallbacks = [];
|
|
7
6
|
const mouseEventsToUpdateOn = ["mousedown", "mousemove", "wheel"];
|
|
8
7
|
const isMobile = (_b = (_a = navigator.userAgentData) === null || _a === void 0 ? void 0 : _a.mobile) !== null && _b !== void 0 ? _b : navigator.maxTouchPoints > 1;
|
|
9
8
|
const isiPad = os === "Mac" && isMobile;
|
|
10
9
|
let isSendingCapsLockState = !isiPad;
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
const afterKeyup = new Map();
|
|
11
|
+
function setCapsState(newCapsState) {
|
|
12
|
+
if (capsState !== newCapsState) {
|
|
13
|
+
capsState = newCapsState;
|
|
15
14
|
onCapsChangeCallbacks.forEach((callback) => callback(capsState));
|
|
16
15
|
}
|
|
17
16
|
}
|
|
@@ -24,49 +23,61 @@ mouseEventsToUpdateOn.forEach((eventType) => {
|
|
|
24
23
|
if (!isiPad) {
|
|
25
24
|
const currentCapsState = getCapsLockModifierState(event);
|
|
26
25
|
if (!isMobile || !currentCapsState) {
|
|
27
|
-
|
|
28
|
-
callCallbackIfNeeded();
|
|
26
|
+
setCapsState(currentCapsState);
|
|
29
27
|
}
|
|
30
28
|
}
|
|
31
29
|
});
|
|
32
30
|
});
|
|
33
31
|
document.addEventListener("keyup", (event) => {
|
|
32
|
+
const setAfterKeyupValue = afterKeyup.get(event.code);
|
|
33
|
+
if (setAfterKeyupValue !== undefined) {
|
|
34
|
+
setCapsState(setAfterKeyupValue);
|
|
35
|
+
afterKeyup.delete(event.code);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
34
38
|
if (os === "Mac") {
|
|
35
39
|
if (event.key === "CapsLock") {
|
|
36
|
-
|
|
40
|
+
setCapsState(false);
|
|
41
|
+
return;
|
|
37
42
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
isSendingCapsLockState = true;
|
|
43
|
-
}
|
|
43
|
+
const currentCapsState = getCapsLockModifierState(event);
|
|
44
|
+
if (isSendingCapsLockState || currentCapsState) {
|
|
45
|
+
setCapsState(currentCapsState);
|
|
46
|
+
isSendingCapsLockState = true;
|
|
44
47
|
}
|
|
45
48
|
}
|
|
46
|
-
else if (os === "Windows") {
|
|
47
|
-
capsState = getCapsLockModifierState(event);
|
|
48
|
-
}
|
|
49
49
|
else if (event.key !== "CapsLock" && event.key !== "Unidentified") {
|
|
50
|
-
|
|
50
|
+
setCapsState(getCapsLockModifierState(event));
|
|
51
51
|
}
|
|
52
|
-
callCallbackIfNeeded();
|
|
53
52
|
});
|
|
54
53
|
document.addEventListener("keydown", (event) => {
|
|
55
|
-
if (
|
|
54
|
+
if (afterKeyup.get(event.code) !== undefined) {
|
|
55
|
+
afterKeyup.delete(event.code);
|
|
56
|
+
}
|
|
57
|
+
if (os === "Windows") {
|
|
58
|
+
setCapsState(getCapsLockModifierState(event));
|
|
59
|
+
}
|
|
60
|
+
else if (os === "Mac") {
|
|
56
61
|
if (event.key === "CapsLock") {
|
|
57
|
-
|
|
58
|
-
callCallbackIfNeeded();
|
|
62
|
+
setCapsState(true);
|
|
59
63
|
}
|
|
60
64
|
}
|
|
61
65
|
else if (os === "Linux") {
|
|
62
66
|
if (event.key === "CapsLock") {
|
|
63
|
-
|
|
67
|
+
const flippedCapsState = !getCapsLockModifierState(event);
|
|
68
|
+
if (flippedCapsState) {
|
|
69
|
+
setCapsState(true);
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
afterKeyup.set(event.code, flippedCapsState);
|
|
73
|
+
}
|
|
64
74
|
}
|
|
65
75
|
}
|
|
66
76
|
});
|
|
67
|
-
|
|
77
|
+
function isCapsLockOn() {
|
|
68
78
|
return capsState;
|
|
69
79
|
}
|
|
70
|
-
|
|
80
|
+
function onCapsLockChange(callback) {
|
|
71
81
|
onCapsChangeCallbacks.push(callback);
|
|
72
82
|
}
|
|
83
|
+
export { isCapsLockOn, onCapsLockChange };
|