-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.js
58 lines (47 loc) · 1.33 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
import { EventEmitter } from 'events';
import path from 'path'
import { execFile } from 'child_process';
import {fileURLToPath} from 'node:url';
const __dirname = path.dirname(fileURLToPath(import.meta.url))
class ClipboardEventListener extends EventEmitter {
constructor() {
super();
this.child = null;
}
startListening() {
const { platform } = process;
if (platform === 'win32') {
this.child = execFile(path.join(__dirname,'platform/clipboard-event-handler-win32.exe'));
}
else if (platform === 'linux') {
this.child = execFile(path.join(__dirname,'platform/clipboard-event-handler-linux'));
}
else if (platform === 'darwin') {
this.child = execFile(path.join(__dirname,'platform/clipboard-event-handler-mac'));
}
else {
throw 'Not yet supported';
}
this.child.stdout.on('data', (data) => {
if (data.trim() === 'CLIPBOARD_CHANGE') {
this.emit('change');
}
});
}
stopListening() {
const res = this.child.kill();
return res;
}
}
export default new ClipboardEventListener();
// Sample usage
/*
import clipboardListener from 'clipboard-event'
// To start listening
clipboardListener.startListening();
clipboardListener.on('change', () => {
console.log('Clipboard changed');
});
// To stop listening
clipboardListener.stopListening();
*/