-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.cjs
56 lines (45 loc) · 1.26 KB
/
index.cjs
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
const { EventEmitter } = require('events');
const path = require('path');
const { execFile } = require('child_process');
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;
}
}
module.exports = new ClipboardEventListener();
// Sample usage
/*
const clipboardListener = require('clipboard-event');
// To start listening
clipboardListener.startListening();
clipboardListener.on('change', () => {
console.log('Clipboard changed');
});
// To stop listening
clipboardListener.stopListening();
*/