-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
151 lines (124 loc) · 4.12 KB
/
main.cpp
File metadata and controls
151 lines (124 loc) · 4.12 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
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
// CPCD App
#include <iostream>
#include <vector>
#define VERSION "0.1.4"
#ifdef _WIN32
#include <windows.h>
#elif __linux__
#include <unistd.h>
#endif
HHOOK hKeyboardHook;
bool blockInput = true;
bool rightCtrlPressed = false;
bool rightAltPressed = false;
std::vector<HWND> monitorWindows;
static LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode == HC_ACTION) {
auto* pKeyboard = reinterpret_cast<KBDLLHOOKSTRUCT *>(lParam);
if (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN) {
if (pKeyboard->vkCode == VK_RCONTROL) {
rightCtrlPressed = true;
}
if (pKeyboard->vkCode == VK_RMENU) {
rightAltPressed = true;
}
if (rightCtrlPressed && rightAltPressed) {
blockInput = false;
std::cout << "Input unblocked!" << std::endl;
UnhookWindowsHookEx(hKeyboardHook);
return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
}
}
if (wParam == WM_KEYUP || wParam == WM_SYSKEYUP) {
if (pKeyboard->vkCode == VK_RCONTROL) {
rightCtrlPressed = false;
}
if (pKeyboard->vkCode == VK_RMENU) {
rightAltPressed = false;
}
}
if (blockInput) {
return 1;
}
}
return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
}
static LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_DESTROY:
UnhookWindowsHookEx(hKeyboardHook);
ShowCursor(TRUE);
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
}
BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) {
(void)hdcMonitor;
(void)lprcMonitor;
(void)dwData;
MONITORINFO mi = { sizeof(MONITORINFO) };
if (GetMonitorInfo(hMonitor, &mi)) {
int x = mi.rcMonitor.left;
int y = mi.rcMonitor.top;
int width = mi.rcMonitor.right - mi.rcMonitor.left;
int height = mi.rcMonitor.bottom - mi.rcMonitor.top;
HWND hwnd = CreateWindowEx(WS_EX_LAYERED | WS_EX_TOOLWINDOW,
"FullScreenClass", nullptr, WS_POPUP | WS_VISIBLE,
x, y, width, height, nullptr, nullptr,
GetModuleHandle(nullptr), nullptr);
if (hwnd) {
SetLayeredWindowAttributes(hwnd, 0, 255, LWA_ALPHA);
SetWindowPos(hwnd, HWND_TOPMOST, x, y, width, height, SWP_SHOWWINDOW);
monitorWindows.push_back(hwnd);
}
}
return TRUE;
}
static void cpcd() {
#ifdef _WIN32
WNDCLASS wc = {};
wc.lpfnWndProc = WindowProc;
wc.hInstance = GetModuleHandle(nullptr);
wc.lpszClassName = "FullScreenClass";
wc.hCursor = nullptr;
RegisterClass(&wc);
monitorWindows.clear();
EnumDisplayMonitors(nullptr, nullptr, MonitorEnumProc, 0);
if (monitorWindows.empty()) {
std::cerr << "Failed to create any windows!" << std::endl;
exit(EXIT_FAILURE);
}
ShowCursor(FALSE);
hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, nullptr, 0);
if (!hKeyboardHook) {
std::cerr << "Failed to set keyboard hook!" << std::endl;
exit(EXIT_FAILURE);
}
MSG msg = {};
while (GetMessage(&msg, nullptr, 0, 0)) {
if (!blockInput) {
ShowCursor(TRUE);
for (HWND hwnd : monitorWindows) {
DestroyWindow(hwnd);
}
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
std::cout << "CPCD on Windows! Version: " << VERSION << std::endl;
#elif __linux__
// ! added later !
std::cout << "CPCD on Linux - functionality not yet implemented in version " << VERSION << std::endl;
#endif
}
int WINAPI WinMain( _In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nCmdShow) {
(void)hInstance;
(void)hPrevInstance;
(void)lpCmdLine;
(void)nCmdShow;
cpcd();
return 0;
}