@leonabcd123/modern-caps-lock 3.0.5 → 3.1.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # Modern Caps Lock
2
2
 
3
- Modern Caps Lock provides an easy way to check whether Caps Lock is active or not, and it allows you to run your code whenever Caps Lock state changes.
3
+ Modern Caps Lock provides an easy way to check whether Caps Lock is active, and it allows you to run your code whenever the Caps Lock state changes.
4
4
 
5
5
  1. [Installation](#installation)
6
6
  2. [API](#api)
@@ -13,11 +13,9 @@ Modern Caps Lock provides an easy way to check whether Caps Lock is active or no
13
13
  3. [Examples](#examples)
14
14
  1. [Print Caps Lock state after every change](#print-caps-lock-state-after-every-change)
15
15
  2. [Get current Caps Lock state](#get-current-caps-lock-state)
16
- 5. [Support](#support)
17
- 1. [Supported Platforms](#supported-platforms)
18
- 2. [Unsupported Platforms](#unsupported-platforms)
19
- 6. [Limitations](#limitations)
20
- 7. [Credits](#credits)
16
+ 4. [Supported Platforms](#supported-platforms)
17
+ 5. [Limitations](#limitations)
18
+ 6. [Credits](#credits)
21
19
 
22
20
  ### Installation
23
21
 
@@ -31,7 +29,7 @@ npm install @leonabcd123/modern-caps-lock
31
29
 
32
30
  #### onCapsLockChange
33
31
 
34
- Runs the provided callback function whenever Caps Lock state is changed.
32
+ Runs the provided callback function whenever the Caps Lock state changes.
35
33
 
36
34
  ##### Arguments
37
35
 
@@ -51,7 +49,7 @@ None.
51
49
 
52
50
  ##### Return value
53
51
 
54
- `capsState: boolean`: a boolean representing the current Caps Lock state. If `true`, Caps Lock is on; if `false`, Caps Lock is off.
52
+ `capsState: boolean`: a boolean indicating whether Caps Lock is on. If `true`, Caps Lock is on; if `false`, Caps Lock is off.
55
53
 
56
54
 
57
55
  ### Examples
@@ -77,23 +75,18 @@ if (isCapsLockOn()) {
77
75
  }
78
76
  ```
79
77
 
80
- ### Support
81
-
82
- ##### Supported Platforms
78
+ ### Supported Platforms
83
79
 
84
80
  - Windows
85
81
  - Mac
86
82
  - Linux
87
83
  - iPad
88
-
89
- ##### Unsupported Platforms
90
-
91
- - Platforms using [GBoard](https://en.wikipedia.org/wiki/Gboard)
84
+ - Android
92
85
 
93
86
  ### Limitations
94
87
 
95
88
  Because of browser limitations, we can only detect the Caps Lock state after a KeyboardEvent
96
- or a MouseEvent. We currently detect updates to the Caps Lock state when the following events are fired:
89
+ or MouseEvent occurs. We currently detect updates to the Caps Lock state when the following events are fired:
97
90
 
98
91
  - keydown
99
92
  - keyup
@@ -101,7 +94,7 @@ or a MouseEvent. We currently detect updates to the Caps Lock state when the fol
101
94
  - mousemove
102
95
  - wheel
103
96
 
104
- Before any of these events are fired, Caps Lock state is off.
97
+ Until one of these events is fired, the Caps Lock state defaults to `false`.
105
98
 
106
99
  ### Credits
107
100
 
package/lib/index.js CHANGED
@@ -1,26 +1,11 @@
1
- function isPlatform(osName) {
2
- var _a, _b;
3
- return osName.test((_b = (_a = navigator.userAgentData) === null || _a === void 0 ? void 0 : _a.platform) !== null && _b !== void 0 ? _b : (navigator.oscpu || navigator.userAgent || navigator.platform));
4
- }
5
- function getCurrentOs() {
6
- if (isPlatform(/Mac/i)) {
7
- return "Mac";
8
- }
9
- if (isPlatform(/Linux/i)) {
10
- return "Linux";
11
- }
12
- if (isPlatform(/Win/i)) {
13
- return "Windows";
14
- }
15
- return "Unknown";
16
- }
1
+ import { getCurrentOs } from "./os-detection";
17
2
  let previousCapsState = false;
18
3
  let capsState = false;
19
4
  const os = getCurrentOs();
20
5
  let onCapsChangeCallback;
21
6
  const mouseEventsToUpdateOn = ["mousedown", "mousemove", "wheel"];
22
7
  const isiPad = os === "Mac" && navigator.maxTouchPoints > 1;
23
- let isSendingCapsLockStateOniPad = !isiPad;
8
+ let isSendingCapsLockState = !isiPad;
24
9
  function callCallbackIfNeeded() {
25
10
  const callCallback = previousCapsState !== capsState;
26
11
  previousCapsState = capsState;
@@ -34,11 +19,9 @@ function getCapsLockModifierState(event) {
34
19
  }
35
20
  mouseEventsToUpdateOn.forEach((eventType) => {
36
21
  document.addEventListener(eventType, (event) => {
37
- if (event instanceof MouseEvent) {
38
- if (!isiPad) {
39
- capsState = getCapsLockModifierState(event);
40
- callCallbackIfNeeded();
41
- }
22
+ if (!isiPad) {
23
+ capsState = getCapsLockModifierState(event);
24
+ callCallbackIfNeeded();
42
25
  }
43
26
  });
44
27
  });
@@ -49,16 +32,16 @@ document.addEventListener("keyup", (event) => {
49
32
  }
50
33
  else {
51
34
  const currentCapsState = getCapsLockModifierState(event);
52
- if (isSendingCapsLockStateOniPad || currentCapsState) {
35
+ if (isSendingCapsLockState || currentCapsState) {
53
36
  capsState = currentCapsState;
54
- isSendingCapsLockStateOniPad = true;
37
+ isSendingCapsLockState = true;
55
38
  }
56
39
  }
57
40
  }
58
41
  else if (os === "Windows") {
59
42
  capsState = getCapsLockModifierState(event);
60
43
  }
61
- else if (event.key !== "CapsLock") {
44
+ else if (event.key !== "CapsLock" && event.key !== "Unidentified") {
62
45
  capsState = getCapsLockModifierState(event);
63
46
  }
64
47
  callCallbackIfNeeded();
@@ -0,0 +1 @@
1
+ export declare function getCurrentOs(): "Mac" | "Linux" | "Windows" | "Unknown";
@@ -0,0 +1,16 @@
1
+ function isPlatform(osName) {
2
+ var _a, _b, _c;
3
+ return osName.test((_b = (_a = navigator.userAgentData) === null || _a === void 0 ? void 0 : _a.platform) !== null && _b !== void 0 ? _b : ((_c = navigator.oscpu) !== null && _c !== void 0 ? _c : "") + navigator.userAgent + navigator.platform);
4
+ }
5
+ export function getCurrentOs() {
6
+ if (isPlatform(/Mac/i)) {
7
+ return "Mac";
8
+ }
9
+ if (isPlatform(/Linux|Android/i)) {
10
+ return "Linux";
11
+ }
12
+ if (isPlatform(/Win/i)) {
13
+ return "Windows";
14
+ }
15
+ return "Unknown";
16
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leonabcd123/modern-caps-lock",
3
- "version": "3.0.5",
3
+ "version": "3.1.0",
4
4
  "description": "A package that allows you to check whether caps lock is active or not",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -29,7 +29,7 @@
29
29
  "contributors": [
30
30
  "fehmer"
31
31
  ],
32
- "license": "GPL-3.0-only",
32
+ "license": "GPL-3.0-or-later",
33
33
  "bugs": {
34
34
  "url": "https://github.com/Leonabcd123/modern-caps-lock/issues"
35
35
  },