simen-keyboard-listener 1.0.7 → 1.0.9
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/LICENSE +21 -21
- package/README.md +105 -69
- package/dist/index.d.mts +7 -2
- package/dist/index.d.ts +7 -2
- package/dist/index.js +113 -17
- package/dist/index.mjs +111 -17
- package/package.json +58 -57
- package/prebuilds/darwin-arm64/simen_keyboard_listener.node +0 -0
- package/prebuilds/win32-x64/simen_keyboard_listener.node +0 -0
- package/src/native/binding.gyp +0 -40
- package/src/native/package.json +0 -14
- package/src/native/simen_keyboard_listener.cc +0 -660
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2024 Simen
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Simen
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,69 +1,105 @@
|
|
|
1
|
-
# simen-keyboard-listener
|
|
2
|
-
|
|
3
|
-
Native global keyboard listener for macOS and Windows. Captures keyboard events system-wide, including special keys like FN that cannot be detected via standard DOM events.
|
|
4
|
-
|
|
5
|
-
## Installation
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
npm install simen-keyboard-listener
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
listener
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
##
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
1
|
+
# simen-keyboard-listener
|
|
2
|
+
|
|
3
|
+
Native global keyboard listener for macOS and Windows. Captures keyboard events system-wide, including special keys like FN that cannot be detected via standard DOM events.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install simen-keyboard-listener
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
The package uses `optionalDependencies` to automatically install the correct native binary for your platform. Supported platforms:
|
|
12
|
+
|
|
13
|
+
| Package | Platform |
|
|
14
|
+
|---------|----------|
|
|
15
|
+
| `@simen-keyboard-listener/darwin-arm64` | macOS Apple Silicon |
|
|
16
|
+
| `@simen-keyboard-listener/darwin-x64` | macOS Intel |
|
|
17
|
+
| `@simen-keyboard-listener/win32-x64` | Windows x64 |
|
|
18
|
+
| `@simen-keyboard-listener/win32-arm64` | Windows ARM64 |
|
|
19
|
+
|
|
20
|
+
### For Electron/Bundler Projects
|
|
21
|
+
|
|
22
|
+
If you're using webpack, electron-builder, or other bundlers, you may need to configure them to include the native addon:
|
|
23
|
+
|
|
24
|
+
**electron-builder (package.json):**
|
|
25
|
+
```json
|
|
26
|
+
{
|
|
27
|
+
"build": {
|
|
28
|
+
"files": [
|
|
29
|
+
"node_modules/@simen-keyboard-listener/**/*"
|
|
30
|
+
]
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
**webpack (webpack.config.js):**
|
|
36
|
+
```javascript
|
|
37
|
+
module.exports = {
|
|
38
|
+
externals: {
|
|
39
|
+
'@simen-keyboard-listener/darwin-arm64': 'commonjs @simen-keyboard-listener/darwin-arm64',
|
|
40
|
+
'@simen-keyboard-listener/darwin-x64': 'commonjs @simen-keyboard-listener/darwin-x64',
|
|
41
|
+
'@simen-keyboard-listener/win32-x64': 'commonjs @simen-keyboard-listener/win32-x64',
|
|
42
|
+
'@simen-keyboard-listener/win32-arm64': 'commonjs @simen-keyboard-listener/win32-arm64',
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Usage
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
import { getGlobalKeyboardListener } from 'simen-keyboard-listener';
|
|
51
|
+
|
|
52
|
+
const listener = getGlobalKeyboardListener();
|
|
53
|
+
|
|
54
|
+
listener.addListener((event, isDown) => {
|
|
55
|
+
console.log(`Key: ${event.name}, State: ${event.state}`);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
// Later, to stop listening:
|
|
59
|
+
listener.removeListener(myListener);
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## API
|
|
63
|
+
|
|
64
|
+
### `getGlobalKeyboardListener(): IGlobalKeyboardListener`
|
|
65
|
+
|
|
66
|
+
Returns the singleton keyboard listener instance.
|
|
67
|
+
|
|
68
|
+
### `IGlobalKeyboardListener`
|
|
69
|
+
|
|
70
|
+
- `addListener(listener: IGlobalKeyListener): void` - Add a key event listener
|
|
71
|
+
- `removeListener(listener: IGlobalKeyListener): void` - Remove a listener
|
|
72
|
+
- `kill(): void` - Stop the listener (only when no listeners remain)
|
|
73
|
+
|
|
74
|
+
### `IGlobalKeyEvent`
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
interface IGlobalKeyEvent {
|
|
78
|
+
readonly name: string; // e.g. "FN", "LEFT SHIFT", "A", "ESCAPE"
|
|
79
|
+
readonly state: 'DOWN' | 'UP';
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Supported Keys
|
|
84
|
+
|
|
85
|
+
- **Modifiers**: FN (macOS), SHIFT, CTRL, ALT, META (Cmd/Win)
|
|
86
|
+
- **Letters**: A-Z
|
|
87
|
+
- **Numbers**: 0-9
|
|
88
|
+
- **Function keys**: F1-F12 (Windows)
|
|
89
|
+
- **Special**: ESCAPE, SPACE, RETURN, TAB, BACKSPACE, DELETE
|
|
90
|
+
- **Arrows**: UP, DOWN, LEFT, RIGHT
|
|
91
|
+
|
|
92
|
+
## Platform Support
|
|
93
|
+
|
|
94
|
+
- macOS (x64, arm64)
|
|
95
|
+
- Windows (x64, arm64)
|
|
96
|
+
|
|
97
|
+
## Requirements
|
|
98
|
+
|
|
99
|
+
- Node.js >= 18.0.0
|
|
100
|
+
- macOS: Accessibility permission required
|
|
101
|
+
- Windows: No special permissions needed
|
|
102
|
+
|
|
103
|
+
## License
|
|
104
|
+
|
|
105
|
+
MIT
|
package/dist/index.d.mts
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
|
-
type IGlobalKeyState = 'DOWN' | 'UP';
|
|
1
|
+
type IGlobalKeyState = 'DOWN' | 'UP' | 'PERMISSION_LOST';
|
|
2
2
|
interface IGlobalKeyEvent {
|
|
3
3
|
readonly name: string;
|
|
4
4
|
readonly state: IGlobalKeyState;
|
|
5
5
|
}
|
|
6
6
|
type IGlobalKeyDownMap = Record<string, boolean>;
|
|
7
7
|
type IGlobalKeyListener = (event: IGlobalKeyEvent, isDown: IGlobalKeyDownMap) => void;
|
|
8
|
+
type IPermissionLostListener = () => void;
|
|
8
9
|
interface IGlobalKeyboardListener {
|
|
9
10
|
addListener(listener: IGlobalKeyListener): void;
|
|
10
11
|
removeListener(listener: IGlobalKeyListener): void;
|
|
12
|
+
onPermissionLost(listener: IPermissionLostListener): void;
|
|
13
|
+
removePermissionLostListener(listener: IPermissionLostListener): void;
|
|
11
14
|
kill(): void;
|
|
12
15
|
}
|
|
13
16
|
/**
|
|
@@ -32,5 +35,7 @@ declare function createGlobalKeyboardListener(): IGlobalKeyboardListener;
|
|
|
32
35
|
* Call this before starting the listener to provide better UX when permission is missing.
|
|
33
36
|
*/
|
|
34
37
|
declare function checkKeyboardPermission(): boolean;
|
|
38
|
+
declare function getFocusedInputValue(): string | null;
|
|
39
|
+
declare function getFocusedInputSelectedText(): string | null;
|
|
35
40
|
|
|
36
|
-
export { type IGlobalKeyDownMap, type IGlobalKeyEvent, type IGlobalKeyListener, type IGlobalKeyState, type IGlobalKeyboardListener, checkKeyboardPermission, createGlobalKeyboardListener, getGlobalKeyboardListener };
|
|
41
|
+
export { type IGlobalKeyDownMap, type IGlobalKeyEvent, type IGlobalKeyListener, type IGlobalKeyState, type IGlobalKeyboardListener, type IPermissionLostListener, checkKeyboardPermission, createGlobalKeyboardListener, getFocusedInputSelectedText, getFocusedInputValue, getGlobalKeyboardListener };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
|
-
type IGlobalKeyState = 'DOWN' | 'UP';
|
|
1
|
+
type IGlobalKeyState = 'DOWN' | 'UP' | 'PERMISSION_LOST';
|
|
2
2
|
interface IGlobalKeyEvent {
|
|
3
3
|
readonly name: string;
|
|
4
4
|
readonly state: IGlobalKeyState;
|
|
5
5
|
}
|
|
6
6
|
type IGlobalKeyDownMap = Record<string, boolean>;
|
|
7
7
|
type IGlobalKeyListener = (event: IGlobalKeyEvent, isDown: IGlobalKeyDownMap) => void;
|
|
8
|
+
type IPermissionLostListener = () => void;
|
|
8
9
|
interface IGlobalKeyboardListener {
|
|
9
10
|
addListener(listener: IGlobalKeyListener): void;
|
|
10
11
|
removeListener(listener: IGlobalKeyListener): void;
|
|
12
|
+
onPermissionLost(listener: IPermissionLostListener): void;
|
|
13
|
+
removePermissionLostListener(listener: IPermissionLostListener): void;
|
|
11
14
|
kill(): void;
|
|
12
15
|
}
|
|
13
16
|
/**
|
|
@@ -32,5 +35,7 @@ declare function createGlobalKeyboardListener(): IGlobalKeyboardListener;
|
|
|
32
35
|
* Call this before starting the listener to provide better UX when permission is missing.
|
|
33
36
|
*/
|
|
34
37
|
declare function checkKeyboardPermission(): boolean;
|
|
38
|
+
declare function getFocusedInputValue(): string | null;
|
|
39
|
+
declare function getFocusedInputSelectedText(): string | null;
|
|
35
40
|
|
|
36
|
-
export { type IGlobalKeyDownMap, type IGlobalKeyEvent, type IGlobalKeyListener, type IGlobalKeyState, type IGlobalKeyboardListener, checkKeyboardPermission, createGlobalKeyboardListener, getGlobalKeyboardListener };
|
|
41
|
+
export { type IGlobalKeyDownMap, type IGlobalKeyEvent, type IGlobalKeyListener, type IGlobalKeyState, type IGlobalKeyboardListener, type IPermissionLostListener, checkKeyboardPermission, createGlobalKeyboardListener, getFocusedInputSelectedText, getFocusedInputValue, getGlobalKeyboardListener };
|
package/dist/index.js
CHANGED
|
@@ -32,14 +32,27 @@ var index_exports = {};
|
|
|
32
32
|
__export(index_exports, {
|
|
33
33
|
checkKeyboardPermission: () => checkKeyboardPermission,
|
|
34
34
|
createGlobalKeyboardListener: () => createGlobalKeyboardListener,
|
|
35
|
+
getFocusedInputSelectedText: () => getFocusedInputSelectedText,
|
|
36
|
+
getFocusedInputValue: () => getFocusedInputValue,
|
|
35
37
|
getGlobalKeyboardListener: () => getGlobalKeyboardListener
|
|
36
38
|
});
|
|
37
39
|
module.exports = __toCommonJS(index_exports);
|
|
38
40
|
var path = __toESM(require("path"));
|
|
39
41
|
var fs = __toESM(require("fs"));
|
|
42
|
+
var import_node_child_process = require("child_process");
|
|
40
43
|
var import_node_module = require("module");
|
|
41
44
|
var import_url = require("url");
|
|
42
45
|
var import_meta = {};
|
|
46
|
+
var PLATFORM_PACKAGES = {
|
|
47
|
+
"darwin-arm64": "@simen-keyboard-listener/darwin-arm64",
|
|
48
|
+
"win32-x64": "@simen-keyboard-listener/win32-x64"
|
|
49
|
+
};
|
|
50
|
+
function getRequire() {
|
|
51
|
+
if (typeof import_meta !== "undefined" && import_meta.url) {
|
|
52
|
+
return (0, import_node_module.createRequire)(import_meta.url);
|
|
53
|
+
}
|
|
54
|
+
return require;
|
|
55
|
+
}
|
|
43
56
|
function getModulePaths() {
|
|
44
57
|
if (typeof import_meta !== "undefined" && import_meta.url) {
|
|
45
58
|
const filename = (0, import_url.fileURLToPath)(import_meta.url);
|
|
@@ -53,38 +66,42 @@ function getModulePaths() {
|
|
|
53
66
|
};
|
|
54
67
|
}
|
|
55
68
|
var modulePaths = getModulePaths();
|
|
56
|
-
|
|
69
|
+
var nativeAddon = null;
|
|
70
|
+
function getNativeAddon() {
|
|
71
|
+
if (nativeAddon) {
|
|
72
|
+
return nativeAddon;
|
|
73
|
+
}
|
|
57
74
|
const platformArch = `${process.platform}-${process.arch}`;
|
|
75
|
+
const packageName = PLATFORM_PACKAGES[platformArch];
|
|
76
|
+
const localRequire = getRequire();
|
|
77
|
+
if (packageName) {
|
|
78
|
+
try {
|
|
79
|
+
const candidate = localRequire(packageName);
|
|
80
|
+
if (candidate && typeof candidate.start === "function" && typeof candidate.stop === "function" && typeof candidate.isRunning === "function" && typeof candidate.checkPermission === "function" && typeof candidate.getFocusedInputValue === "function" && typeof candidate.getFocusedInputSelectedText === "function") {
|
|
81
|
+
nativeAddon = candidate;
|
|
82
|
+
return nativeAddon;
|
|
83
|
+
}
|
|
84
|
+
} catch {
|
|
85
|
+
}
|
|
86
|
+
}
|
|
58
87
|
const baseDir = modulePaths.dirname;
|
|
59
88
|
const possiblePaths = [
|
|
60
|
-
// Prebuilt binaries (preferred)
|
|
61
|
-
path.join(baseDir, "..", "prebuilds", platformArch, "simen-keyboard-listener-native.node"),
|
|
62
|
-
path.join(baseDir, "..", "prebuilds", platformArch, "simen_keyboard_listener.node"),
|
|
63
|
-
// Build output from node-gyp
|
|
64
89
|
path.join(baseDir, "native", "build", "Release", "simen_keyboard_listener.node"),
|
|
65
90
|
path.join(baseDir, "native", "build", "Debug", "simen_keyboard_listener.node"),
|
|
66
|
-
// Source native directory (for development)
|
|
67
91
|
path.join(baseDir, "..", "src", "native", "build", "Release", "simen_keyboard_listener.node"),
|
|
68
92
|
path.join(baseDir, "..", "src", "native", "build", "Debug", "simen_keyboard_listener.node")
|
|
69
93
|
];
|
|
70
94
|
for (const addonPath of possiblePaths) {
|
|
71
95
|
if (fs.existsSync(addonPath)) {
|
|
72
|
-
|
|
96
|
+
nativeAddon = localRequire(addonPath);
|
|
97
|
+
return nativeAddon;
|
|
73
98
|
}
|
|
74
99
|
}
|
|
100
|
+
const supportedPlatforms = Object.keys(PLATFORM_PACKAGES).join(", ");
|
|
75
101
|
throw new Error(
|
|
76
|
-
`simen-keyboard-listener: Native addon not found for ${platformArch}.
|
|
102
|
+
`simen-keyboard-listener: Native addon not found for ${platformArch}. Supported platforms: ${supportedPlatforms}. Please ensure the correct platform package is installed.`
|
|
77
103
|
);
|
|
78
104
|
}
|
|
79
|
-
var nativeAddon = null;
|
|
80
|
-
function getNativeAddon() {
|
|
81
|
-
if (nativeAddon) {
|
|
82
|
-
return nativeAddon;
|
|
83
|
-
}
|
|
84
|
-
const addonPath = getNativeAddonPath();
|
|
85
|
-
nativeAddon = modulePaths.require(addonPath);
|
|
86
|
-
return nativeAddon;
|
|
87
|
-
}
|
|
88
105
|
var NativeKeyboardListener = class _NativeKeyboardListener {
|
|
89
106
|
constructor(addon) {
|
|
90
107
|
this.addon = addon;
|
|
@@ -92,6 +109,7 @@ var NativeKeyboardListener = class _NativeKeyboardListener {
|
|
|
92
109
|
static _instance = null;
|
|
93
110
|
started = false;
|
|
94
111
|
listeners = /* @__PURE__ */ new Set();
|
|
112
|
+
permissionLostListeners = /* @__PURE__ */ new Set();
|
|
95
113
|
static getInstance() {
|
|
96
114
|
if (!_NativeKeyboardListener._instance) {
|
|
97
115
|
const addon = getNativeAddon();
|
|
@@ -103,6 +121,10 @@ var NativeKeyboardListener = class _NativeKeyboardListener {
|
|
|
103
121
|
this.listeners.add(listener);
|
|
104
122
|
if (!this.started) {
|
|
105
123
|
const ok = this.addon.start((event) => {
|
|
124
|
+
if (event.state === "PERMISSION_LOST") {
|
|
125
|
+
this.handlePermissionLost();
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
106
128
|
const listenersCopy = Array.from(this.listeners);
|
|
107
129
|
for (const cb of listenersCopy) {
|
|
108
130
|
try {
|
|
@@ -117,6 +139,22 @@ var NativeKeyboardListener = class _NativeKeyboardListener {
|
|
|
117
139
|
this.started = true;
|
|
118
140
|
}
|
|
119
141
|
}
|
|
142
|
+
onPermissionLost(listener) {
|
|
143
|
+
this.permissionLostListeners.add(listener);
|
|
144
|
+
}
|
|
145
|
+
removePermissionLostListener(listener) {
|
|
146
|
+
this.permissionLostListeners.delete(listener);
|
|
147
|
+
}
|
|
148
|
+
handlePermissionLost() {
|
|
149
|
+
this.started = false;
|
|
150
|
+
const listenersCopy = Array.from(this.permissionLostListeners);
|
|
151
|
+
for (const cb of listenersCopy) {
|
|
152
|
+
try {
|
|
153
|
+
cb();
|
|
154
|
+
} catch {
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
120
158
|
removeListener(listener) {
|
|
121
159
|
this.listeners.delete(listener);
|
|
122
160
|
if (this.listeners.size === 0 && this.started) {
|
|
@@ -149,6 +187,7 @@ var NativeKeyboardListener = class _NativeKeyboardListener {
|
|
|
149
187
|
} finally {
|
|
150
188
|
this.started = false;
|
|
151
189
|
this.listeners.clear();
|
|
190
|
+
this.permissionLostListeners.clear();
|
|
152
191
|
_NativeKeyboardListener._instance = null;
|
|
153
192
|
}
|
|
154
193
|
}
|
|
@@ -173,9 +212,66 @@ function checkKeyboardPermission() {
|
|
|
173
212
|
return false;
|
|
174
213
|
}
|
|
175
214
|
}
|
|
215
|
+
var lastMacAccessibilitySettingsOpenTs = 0;
|
|
216
|
+
function openMacAccessibilitySettings() {
|
|
217
|
+
if (process.platform !== "darwin") {
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
const now = Date.now();
|
|
221
|
+
if (now - lastMacAccessibilitySettingsOpenTs < 15e3) {
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
lastMacAccessibilitySettingsOpenTs = now;
|
|
225
|
+
try {
|
|
226
|
+
(0, import_node_child_process.execFileSync)("open", ["x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility"], {
|
|
227
|
+
stdio: "ignore"
|
|
228
|
+
});
|
|
229
|
+
} catch {
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
function ensureAccessibilityPermission(addon) {
|
|
233
|
+
try {
|
|
234
|
+
if (addon.checkPermission()) {
|
|
235
|
+
return true;
|
|
236
|
+
}
|
|
237
|
+
} catch {
|
|
238
|
+
}
|
|
239
|
+
openMacAccessibilitySettings();
|
|
240
|
+
return false;
|
|
241
|
+
}
|
|
242
|
+
function getFocusedInputValue() {
|
|
243
|
+
if (process.platform !== "darwin") {
|
|
244
|
+
return null;
|
|
245
|
+
}
|
|
246
|
+
const addon = getNativeAddon();
|
|
247
|
+
if (!ensureAccessibilityPermission(addon)) {
|
|
248
|
+
return null;
|
|
249
|
+
}
|
|
250
|
+
try {
|
|
251
|
+
return addon.getFocusedInputValue();
|
|
252
|
+
} catch {
|
|
253
|
+
return null;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
function getFocusedInputSelectedText() {
|
|
257
|
+
if (process.platform !== "darwin") {
|
|
258
|
+
return null;
|
|
259
|
+
}
|
|
260
|
+
const addon = getNativeAddon();
|
|
261
|
+
if (!ensureAccessibilityPermission(addon)) {
|
|
262
|
+
return null;
|
|
263
|
+
}
|
|
264
|
+
try {
|
|
265
|
+
return addon.getFocusedInputSelectedText();
|
|
266
|
+
} catch {
|
|
267
|
+
return null;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
176
270
|
// Annotate the CommonJS export names for ESM import in node:
|
|
177
271
|
0 && (module.exports = {
|
|
178
272
|
checkKeyboardPermission,
|
|
179
273
|
createGlobalKeyboardListener,
|
|
274
|
+
getFocusedInputSelectedText,
|
|
275
|
+
getFocusedInputValue,
|
|
180
276
|
getGlobalKeyboardListener
|
|
181
277
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -8,8 +8,19 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
|
|
|
8
8
|
// src/index.ts
|
|
9
9
|
import * as path from "path";
|
|
10
10
|
import * as fs from "fs";
|
|
11
|
+
import { execFileSync } from "child_process";
|
|
11
12
|
import { createRequire } from "module";
|
|
12
13
|
import { fileURLToPath } from "url";
|
|
14
|
+
var PLATFORM_PACKAGES = {
|
|
15
|
+
"darwin-arm64": "@simen-keyboard-listener/darwin-arm64",
|
|
16
|
+
"win32-x64": "@simen-keyboard-listener/win32-x64"
|
|
17
|
+
};
|
|
18
|
+
function getRequire() {
|
|
19
|
+
if (typeof import.meta !== "undefined" && import.meta.url) {
|
|
20
|
+
return createRequire(import.meta.url);
|
|
21
|
+
}
|
|
22
|
+
return __require;
|
|
23
|
+
}
|
|
13
24
|
function getModulePaths() {
|
|
14
25
|
if (typeof import.meta !== "undefined" && import.meta.url) {
|
|
15
26
|
const filename = fileURLToPath(import.meta.url);
|
|
@@ -23,38 +34,42 @@ function getModulePaths() {
|
|
|
23
34
|
};
|
|
24
35
|
}
|
|
25
36
|
var modulePaths = getModulePaths();
|
|
26
|
-
|
|
37
|
+
var nativeAddon = null;
|
|
38
|
+
function getNativeAddon() {
|
|
39
|
+
if (nativeAddon) {
|
|
40
|
+
return nativeAddon;
|
|
41
|
+
}
|
|
27
42
|
const platformArch = `${process.platform}-${process.arch}`;
|
|
43
|
+
const packageName = PLATFORM_PACKAGES[platformArch];
|
|
44
|
+
const localRequire = getRequire();
|
|
45
|
+
if (packageName) {
|
|
46
|
+
try {
|
|
47
|
+
const candidate = localRequire(packageName);
|
|
48
|
+
if (candidate && typeof candidate.start === "function" && typeof candidate.stop === "function" && typeof candidate.isRunning === "function" && typeof candidate.checkPermission === "function" && typeof candidate.getFocusedInputValue === "function" && typeof candidate.getFocusedInputSelectedText === "function") {
|
|
49
|
+
nativeAddon = candidate;
|
|
50
|
+
return nativeAddon;
|
|
51
|
+
}
|
|
52
|
+
} catch {
|
|
53
|
+
}
|
|
54
|
+
}
|
|
28
55
|
const baseDir = modulePaths.dirname;
|
|
29
56
|
const possiblePaths = [
|
|
30
|
-
// Prebuilt binaries (preferred)
|
|
31
|
-
path.join(baseDir, "..", "prebuilds", platformArch, "simen-keyboard-listener-native.node"),
|
|
32
|
-
path.join(baseDir, "..", "prebuilds", platformArch, "simen_keyboard_listener.node"),
|
|
33
|
-
// Build output from node-gyp
|
|
34
57
|
path.join(baseDir, "native", "build", "Release", "simen_keyboard_listener.node"),
|
|
35
58
|
path.join(baseDir, "native", "build", "Debug", "simen_keyboard_listener.node"),
|
|
36
|
-
// Source native directory (for development)
|
|
37
59
|
path.join(baseDir, "..", "src", "native", "build", "Release", "simen_keyboard_listener.node"),
|
|
38
60
|
path.join(baseDir, "..", "src", "native", "build", "Debug", "simen_keyboard_listener.node")
|
|
39
61
|
];
|
|
40
62
|
for (const addonPath of possiblePaths) {
|
|
41
63
|
if (fs.existsSync(addonPath)) {
|
|
42
|
-
|
|
64
|
+
nativeAddon = localRequire(addonPath);
|
|
65
|
+
return nativeAddon;
|
|
43
66
|
}
|
|
44
67
|
}
|
|
68
|
+
const supportedPlatforms = Object.keys(PLATFORM_PACKAGES).join(", ");
|
|
45
69
|
throw new Error(
|
|
46
|
-
`simen-keyboard-listener: Native addon not found for ${platformArch}.
|
|
70
|
+
`simen-keyboard-listener: Native addon not found for ${platformArch}. Supported platforms: ${supportedPlatforms}. Please ensure the correct platform package is installed.`
|
|
47
71
|
);
|
|
48
72
|
}
|
|
49
|
-
var nativeAddon = null;
|
|
50
|
-
function getNativeAddon() {
|
|
51
|
-
if (nativeAddon) {
|
|
52
|
-
return nativeAddon;
|
|
53
|
-
}
|
|
54
|
-
const addonPath = getNativeAddonPath();
|
|
55
|
-
nativeAddon = modulePaths.require(addonPath);
|
|
56
|
-
return nativeAddon;
|
|
57
|
-
}
|
|
58
73
|
var NativeKeyboardListener = class _NativeKeyboardListener {
|
|
59
74
|
constructor(addon) {
|
|
60
75
|
this.addon = addon;
|
|
@@ -62,6 +77,7 @@ var NativeKeyboardListener = class _NativeKeyboardListener {
|
|
|
62
77
|
static _instance = null;
|
|
63
78
|
started = false;
|
|
64
79
|
listeners = /* @__PURE__ */ new Set();
|
|
80
|
+
permissionLostListeners = /* @__PURE__ */ new Set();
|
|
65
81
|
static getInstance() {
|
|
66
82
|
if (!_NativeKeyboardListener._instance) {
|
|
67
83
|
const addon = getNativeAddon();
|
|
@@ -73,6 +89,10 @@ var NativeKeyboardListener = class _NativeKeyboardListener {
|
|
|
73
89
|
this.listeners.add(listener);
|
|
74
90
|
if (!this.started) {
|
|
75
91
|
const ok = this.addon.start((event) => {
|
|
92
|
+
if (event.state === "PERMISSION_LOST") {
|
|
93
|
+
this.handlePermissionLost();
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
76
96
|
const listenersCopy = Array.from(this.listeners);
|
|
77
97
|
for (const cb of listenersCopy) {
|
|
78
98
|
try {
|
|
@@ -87,6 +107,22 @@ var NativeKeyboardListener = class _NativeKeyboardListener {
|
|
|
87
107
|
this.started = true;
|
|
88
108
|
}
|
|
89
109
|
}
|
|
110
|
+
onPermissionLost(listener) {
|
|
111
|
+
this.permissionLostListeners.add(listener);
|
|
112
|
+
}
|
|
113
|
+
removePermissionLostListener(listener) {
|
|
114
|
+
this.permissionLostListeners.delete(listener);
|
|
115
|
+
}
|
|
116
|
+
handlePermissionLost() {
|
|
117
|
+
this.started = false;
|
|
118
|
+
const listenersCopy = Array.from(this.permissionLostListeners);
|
|
119
|
+
for (const cb of listenersCopy) {
|
|
120
|
+
try {
|
|
121
|
+
cb();
|
|
122
|
+
} catch {
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
90
126
|
removeListener(listener) {
|
|
91
127
|
this.listeners.delete(listener);
|
|
92
128
|
if (this.listeners.size === 0 && this.started) {
|
|
@@ -119,6 +155,7 @@ var NativeKeyboardListener = class _NativeKeyboardListener {
|
|
|
119
155
|
} finally {
|
|
120
156
|
this.started = false;
|
|
121
157
|
this.listeners.clear();
|
|
158
|
+
this.permissionLostListeners.clear();
|
|
122
159
|
_NativeKeyboardListener._instance = null;
|
|
123
160
|
}
|
|
124
161
|
}
|
|
@@ -143,8 +180,65 @@ function checkKeyboardPermission() {
|
|
|
143
180
|
return false;
|
|
144
181
|
}
|
|
145
182
|
}
|
|
183
|
+
var lastMacAccessibilitySettingsOpenTs = 0;
|
|
184
|
+
function openMacAccessibilitySettings() {
|
|
185
|
+
if (process.platform !== "darwin") {
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
const now = Date.now();
|
|
189
|
+
if (now - lastMacAccessibilitySettingsOpenTs < 15e3) {
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
lastMacAccessibilitySettingsOpenTs = now;
|
|
193
|
+
try {
|
|
194
|
+
execFileSync("open", ["x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility"], {
|
|
195
|
+
stdio: "ignore"
|
|
196
|
+
});
|
|
197
|
+
} catch {
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
function ensureAccessibilityPermission(addon) {
|
|
201
|
+
try {
|
|
202
|
+
if (addon.checkPermission()) {
|
|
203
|
+
return true;
|
|
204
|
+
}
|
|
205
|
+
} catch {
|
|
206
|
+
}
|
|
207
|
+
openMacAccessibilitySettings();
|
|
208
|
+
return false;
|
|
209
|
+
}
|
|
210
|
+
function getFocusedInputValue() {
|
|
211
|
+
if (process.platform !== "darwin") {
|
|
212
|
+
return null;
|
|
213
|
+
}
|
|
214
|
+
const addon = getNativeAddon();
|
|
215
|
+
if (!ensureAccessibilityPermission(addon)) {
|
|
216
|
+
return null;
|
|
217
|
+
}
|
|
218
|
+
try {
|
|
219
|
+
return addon.getFocusedInputValue();
|
|
220
|
+
} catch {
|
|
221
|
+
return null;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
function getFocusedInputSelectedText() {
|
|
225
|
+
if (process.platform !== "darwin") {
|
|
226
|
+
return null;
|
|
227
|
+
}
|
|
228
|
+
const addon = getNativeAddon();
|
|
229
|
+
if (!ensureAccessibilityPermission(addon)) {
|
|
230
|
+
return null;
|
|
231
|
+
}
|
|
232
|
+
try {
|
|
233
|
+
return addon.getFocusedInputSelectedText();
|
|
234
|
+
} catch {
|
|
235
|
+
return null;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
146
238
|
export {
|
|
147
239
|
checkKeyboardPermission,
|
|
148
240
|
createGlobalKeyboardListener,
|
|
241
|
+
getFocusedInputSelectedText,
|
|
242
|
+
getFocusedInputValue,
|
|
149
243
|
getGlobalKeyboardListener
|
|
150
244
|
};
|