-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKey_Jacker.cpp
More file actions
113 lines (83 loc) · 2.9 KB
/
Copy pathKey_Jacker.cpp
File metadata and controls
113 lines (83 loc) · 2.9 KB
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
#pragma once
#include "Key_Jacker.h"
#include <thread>
#include <atomic>
static std::thread hookThread;
static std::thread pollThread;
static std::atomic<bool> inputThreadStop{false};
static std::atomic<bool> hookThreadReady{false};
static int pollRateHz = 250;
static HINSTANCE hookHInstance = nullptr;
static bool installKbHook = false;
static bool installMouseHook = false;
static bool installJoystickHook = false;
static void KJ_HookPumpThreadProc()
{
MSG msg;
PeekMessage(&msg, nullptr, 0, 0, PM_NOREMOVE);
kj_cachedForeground.store(::GetForegroundWindow(), std::memory_order_relaxed);
if (installKbHook)
KJ_Keyboard_Install(hookHInstance);
if (installMouseHook)
KJ_Mouse_Install(hookHInstance);
hookThreadReady.store(true, std::memory_order_release);
while (!inputThreadStop.load(std::memory_order_acquire))
{
MsgWaitForMultipleObjects(0, nullptr, FALSE, 50, QS_ALLINPUT);
kj_cachedForeground.store(::GetForegroundWindow(), std::memory_order_relaxed);
while (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
if (installKbHook)
KJ_Keyboard_Uninstall();
if (installMouseHook)
KJ_Mouse_Uninstall();
}
static void KJ_PollDispatchThreadProc()
{
timeBeginPeriod(1);
KJ_InputThreadActive.store(true, std::memory_order_release);
const DWORD pollIntervalMs = (pollRateHz > 0) ? (1000 / pollRateHz) : 4;
while (!inputThreadStop.load(std::memory_order_acquire))
{
Sleep(pollIntervalMs);
Poll_Devices();
Dispatch_Events();
}
KJ_InputThreadActive.store(false, std::memory_order_release);
timeEndPeriod(1);
}
void KJ_Install(HINSTANCE hInstance, HWND targetHwnd, bool EnableKeyboardHook, bool EnableMouseHook, bool EnableJoystickHook)
{
targetGameHwnd = targetHwnd;
hookHInstance = hInstance;
installKbHook = EnableKeyboardHook;
installMouseHook = EnableMouseHook;
installJoystickHook = EnableJoystickHook;
inputThreadStop.store(false);
hookThreadReady.store(false);
KJ_Controls_Reset();
hookThread = std::thread(KJ_HookPumpThreadProc);
SetThreadPriority(reinterpret_cast<HANDLE>(hookThread.native_handle()), THREAD_PRIORITY_HIGHEST);
while (!hookThreadReady.load(std::memory_order_acquire))
Sleep(1);
pollThread = std::thread(KJ_PollDispatchThreadProc);
SetThreadPriority(reinterpret_cast<HANDLE>(pollThread.native_handle()), THREAD_PRIORITY_ABOVE_NORMAL);
if (EnableJoystickHook)
KJ_Joystick_Install();
}
void KJ_Uninstall()
{
inputThreadStop.store(true, std::memory_order_release);
if (pollThread.joinable())
pollThread.join();
if (hookThread.joinable())
hookThread.join();
targetGameHwnd = nullptr;
if (installJoystickHook)
KJ_Joystick_Uninstall();
KJ_Controls_Reset();
}