forked from Kyusung4698/PoE-Overlay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hook.ts
169 lines (142 loc) · 4.67 KB
/
hook.ts
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { addon, Window, windowManager } from 'node-window-manager';
import { IRectangle } from 'node-window-manager/dist/interfaces';
import { Subject, Subscription } from 'rxjs';
import { throttleTime } from 'rxjs/operators';
windowManager.requestAccessibility();
interface KeyboardEvent {
ctrlKey: boolean;
altKey: boolean;
shiftKey: boolean;
keycode: number;
}
interface WheelEvent {
rotation: number;
ctrlKey: boolean;
}
let active = false;
let activeWindow: Window = null;
let bounds: IRectangle = null;
let activeChangeFn: (active: boolean, game: Window, bounds: IRectangle) => void;
let wheelChangeFn: (event: WheelEvent) => void;
const activeCheck$ = new Subject<void>();
let activeCheckSubscription: Subscription;
function onKeydown(event: KeyboardEvent): void {
activeCheck$.next();
}
function onKeyup(event: KeyboardEvent): void {
activeCheck$.next();
}
function onMousewheel(event: WheelEvent): void {
activeCheck$.next();
if (active) {
if (wheelChangeFn) {
wheelChangeFn(event);
}
}
}
function onMouseclick(event: WheelEvent): void {
activeCheck$.next();
}
function checkActive(): void {
let orgActive = active;
let orgActiveWindow = activeWindow;
let orgBounds = bounds;
const possibleWindow = windowManager.getActiveWindow();
if (possibleWindow?.path) {
const title = possibleWindow.getTitle();
if (title === 'Path of Exile') {
const lowerPath = possibleWindow.path.toLowerCase();
active = lowerPath.endsWith('pathofexile_x64_kg.exe') || lowerPath.endsWith('pathofexile_kg.exe')
|| lowerPath.endsWith('pathofexile_x64steam.exe') || lowerPath.endsWith('pathofexilesteam.exe')
|| lowerPath.endsWith('pathofexile_x64.exe') || lowerPath.endsWith('pathofexile.exe');
if (active) {
activeWindow = possibleWindow;
if (addon) {
bounds = addon.getWindowBounds(activeWindow.id);
}
}
} else {
active = false;
}
} else {
active = false;
}
if (orgActive !== active ||
orgActiveWindow?.processId !== activeWindow?.processId ||
JSON.stringify(orgBounds) !== JSON.stringify(bounds)) {
if (activeChangeFn) {
activeChangeFn(active, activeWindow, bounds);
}
}
}
export function getActive(): boolean {
return active;
}
export function on(event: 'change', callback: (active: boolean, game: Window, bounds: IRectangle) => void): void;
export function on(event: 'wheel', callback: (event: WheelEvent) => void): void;
export function on(event: string, callback: any) {
switch (event) {
case 'change':
activeChangeFn = callback;
activeChangeFn(active, activeWindow, bounds);
break;
case 'wheel':
wheelChangeFn = callback;
break;
}
}
export function off(event: 'change' | 'wheel') {
switch (event) {
case 'change':
activeChangeFn = undefined;
break;
case 'wheel':
wheelChangeFn = undefined;
break;
}
}
export function register(): Promise<boolean> {
return new Promise<boolean>(async (resolve) => {
try {
const iohook = (await import('iohook')).default;
iohook.start();
activeCheckSubscription = activeCheck$.pipe(
throttleTime(500, undefined, {
trailing: true,
leading: false
})
).subscribe(() => {
checkActive();
});
iohook.on('keydown', onKeydown);
iohook.on('keyup', onKeyup);
iohook.on('mousewheel', onMousewheel);
iohook.on('mouseup', onMouseclick);
resolve(true);
}
catch (error) {
console.error('An unexpected error occured while registering iohook', error);
resolve(false);
}
})
}
export function unregister(): Promise<void> {
return new Promise(async (resolve, reject) => {
try {
const iohook = (await import('iohook')).default;
iohook.off('keydown', onKeydown);
iohook.off('keyup', onKeyup);
iohook.off('mousewheel', onMousewheel);
iohook.off('mouseup', onMouseclick);
if (activeCheckSubscription) {
activeCheckSubscription.unsubscribe();
}
iohook.stop();
resolve();
}
catch (error) {
console.error('An unexpected error occured while unregistering iohook', error);
reject();
}
});
}