-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhotcorners.cpp
More file actions
251 lines (223 loc) · 9.07 KB
/
Copy pathhotcorners.cpp
File metadata and controls
251 lines (223 loc) · 9.07 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include <windows.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#include <string>
#include <vector>
#include <atomic>
// Constants
const int HOT_CORNER_OFFSET = 20; // Corner offset for trigger
const int DEFAULT_ACTION_DELAY = 60; // Default action delay in milliseconds
const std::string CONFIG_FILE = "hotcorners.ini"; // Configuration file path
// Key mapping table
std::map<std::string, WORD> keyMap = {
// Modifier keys
{"Ctrl", VK_CONTROL}, {"LCtrl", VK_LCONTROL}, {"RCtrl", VK_RCONTROL},
{"Shift", VK_SHIFT}, {"LShift", VK_LSHIFT}, {"RShift", VK_RSHIFT},
{"Alt", VK_MENU}, {"LAlt", VK_LMENU}, {"RAlt", VK_RMENU},
{"Win", VK_LWIN}, {"RWin", VK_RWIN},
// Arrow keys
{"Left", VK_LEFT}, {"Right", VK_RIGHT}, {"Up", VK_UP}, {"Down", VK_DOWN},
// Alphabet keys
{"A", 'A'}, {"B", 'B'}, {"C", 'C'}, {"D", 'D'}, {"E", 'E'}, {"F", 'F'},
{"G", 'G'}, {"H", 'H'}, {"I", 'I'}, {"J", 'J'}, {"K", 'K'}, {"L", 'L'},
{"M", 'M'}, {"N", 'N'}, {"O", 'O'}, {"P", 'P'}, {"Q", 'Q'}, {"R", 'R'},
{"S", 'S'}, {"T", 'T'}, {"U", 'U'}, {"V", 'V'}, {"W", 'W'}, {"X", 'X'},
{"Y", 'Y'}, {"Z", 'Z'},
// Number keys (main keyboard)
{"0", '0'}, {"1", '1'}, {"2", '2'}, {"3", '3'}, {"4", '4'}, {"5", '5'},
{"6", '6'}, {"7", '7'}, {"8", '8'}, {"9", '9'},
// Function keys
{"F1", VK_F1}, {"F2", VK_F2}, {"F3", VK_F3}, {"F4", VK_F4},
{"F5", VK_F5}, {"F6", VK_F6}, {"F7", VK_F7}, {"F8", VK_F8},
{"F9", VK_F9}, {"F10", VK_F10}, {"F11", VK_F11}, {"F12", VK_F12},
// Numpad number keys
{"Num0", VK_NUMPAD0}, {"Num1", VK_NUMPAD1}, {"Num2", VK_NUMPAD2}, {"Num3", VK_NUMPAD3},
{"Num4", VK_NUMPAD4}, {"Num5", VK_NUMPAD5}, {"Num6", VK_NUMPAD6}, {"Num7", VK_NUMPAD7},
{"Num8", VK_NUMPAD8}, {"Num9", VK_NUMPAD9},
// Numpad operation keys
{"NumLk", VK_NUMLOCK}, {"Div", VK_DIVIDE}, {"Mul", VK_MULTIPLY},
{"Sub", VK_SUBTRACT}, {"Add", VK_ADD}, {"Dec", VK_DECIMAL},
// Control keys
{"Esc", VK_ESCAPE}, {"Tab", VK_TAB}, {"Caps", VK_CAPITAL},
{"Space", VK_SPACE}, {"Enter", VK_RETURN}, {"Bksp", VK_BACK},
// Editing keys
{"Ins", VK_INSERT}, {"Del", VK_DELETE}, {"Home", VK_HOME},
{"End", VK_END}, {"PgUp", VK_PRIOR}, {"PgDn", VK_NEXT},
// Punctuation keys
{"SColon", VK_OEM_1}, {"Plus", VK_OEM_PLUS}, {"Comma", VK_OEM_COMMA},
{"Minus", VK_OEM_MINUS}, {"Dot", VK_OEM_PERIOD}, {"Slash", VK_OEM_2},
{"Tilde", VK_OEM_3}, {"LBrack", VK_OEM_4}, {"Bslash", VK_OEM_5},
{"RBrack", VK_OEM_6}, {"Quote", VK_OEM_7},
// Multimedia keys
{"MediaNext", VK_MEDIA_NEXT_TRACK}, {"MediaPrev", VK_MEDIA_PREV_TRACK},
{"MediaStop", VK_MEDIA_STOP}, {"MediaPlayPause", VK_MEDIA_PLAY_PAUSE},
{"VolumeMute", VK_VOLUME_MUTE}, {"VolumeUp", VK_VOLUME_UP},
{"VolumeDown", VK_VOLUME_DOWN},
// Other special keys
{"PrtSc", VK_SNAPSHOT}, {"Scroll", VK_SCROLL}, {"Pause", VK_PAUSE},
{"Menu", VK_APPS} // Application Menu (context menu)
};
// Structure for storing the configuration of each hot corner action
struct CornerAction {
std::string type; // "hotkey" or "cmd"
std::string content; // Key sequence or command line
int delay = DEFAULT_ACTION_DELAY; // Trigger delay in milliseconds
};
// Structure to store the configuration for all four corners
struct HotCornerConfig {
CornerAction topLeft, topRight, bottomLeft, bottomRight;
} hotCornerConfig;
// Function to simulate key press for a series of keys
void PressKeySequence(const std::vector<WORD>& keys) {
std::vector<INPUT> inputs;
for (WORD key : keys) {
INPUT input = {};
input.type = INPUT_KEYBOARD;
input.ki.wVk = key;
inputs.push_back(input);
}
for (WORD key : keys) {
INPUT input = {};
input.type = INPUT_KEYBOARD;
input.ki.wVk = key;
input.ki.dwFlags = KEYEVENTF_KEYUP;
inputs.push_back(input);
}
SendInput(static_cast<UINT>(inputs.size()), inputs.data(), sizeof(INPUT));
Sleep(50);
}
// Function to parse and simulate key sequences from a string
void SimulateKeyPress(const std::string& keySequence) {
std::stringstream ss(keySequence);
std::string group;
while (std::getline(ss, group, ',')) {
std::vector<WORD> keys;
std::stringstream keyStream(group);
std::string key;
while (std::getline(keyStream, key, '+')) {
key = key.substr(key.find_first_not_of(' '));
if (keyMap.count(key)) {
keys.push_back(keyMap[key]);
} else {
std::cerr << "Unknown key: " << key << std::endl;
}
}
if (!keys.empty()) {
PressKeySequence(keys);
}
}
}
// Execute custom command
void ExecuteCommand(const std::string& command) {
STARTUPINFOW si = { sizeof(si) };
PROCESS_INFORMATION pi;
std::wstring commandW(command.begin(), command.end());
if (!CreateProcessW(NULL, &commandW[0], NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
std::wcerr << L"Failed to execute command: " << commandW << std::endl;
} else {
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
}
// Load configuration from the ini file
void LoadConfig() {
std::ifstream config(CONFIG_FILE);
std::string line;
CornerAction* currentAction = nullptr;
while (std::getline(config, line)) {
if (line == "[TopLeft]") currentAction = &hotCornerConfig.topLeft;
else if (line == "[TopRight]") currentAction = &hotCornerConfig.topRight;
else if (line == "[BottomLeft]") currentAction = &hotCornerConfig.bottomLeft;
else if (line == "[BottomRight]") currentAction = &hotCornerConfig.bottomRight;
else if (currentAction) {
if (line.find("type=") == 0)
currentAction->type = line.substr(5);
else if (line.find("content=") == 0)
currentAction->content = line.substr(8);
else if (line.find("delay=") == 0)
currentAction->delay = std::stoi(line.substr(6));
}
}
}
// Execute either a key sequence or a command based on the action type
void ExecuteCornerAction(const CornerAction& action) {
std::cout << "Executing action: " << action.content << std::endl;
if (action.type == "hotkey") {
SimulateKeyPress(action.content);
} else if (action.type == "cmd") {
ExecuteCommand(action.content);
}
}
// Define the rectangle areas of hot corners on screen
const RECT hotCorners[4] = {
{ -HOT_CORNER_OFFSET, -HOT_CORNER_OFFSET, HOT_CORNER_OFFSET, HOT_CORNER_OFFSET },
{ GetSystemMetrics(SM_CXSCREEN) - HOT_CORNER_OFFSET, -HOT_CORNER_OFFSET,
GetSystemMetrics(SM_CXSCREEN) + HOT_CORNER_OFFSET, HOT_CORNER_OFFSET },
{ -HOT_CORNER_OFFSET, GetSystemMetrics(SM_CYSCREEN) - HOT_CORNER_OFFSET,
HOT_CORNER_OFFSET, GetSystemMetrics(SM_CYSCREEN) + HOT_CORNER_OFFSET },
{ GetSystemMetrics(SM_CXSCREEN) - HOT_CORNER_OFFSET,
GetSystemMetrics(SM_CYSCREEN) - HOT_CORNER_OFFSET,
GetSystemMetrics(SM_CXSCREEN) + HOT_CORNER_OFFSET,
GetSystemMetrics(SM_CYSCREEN) + HOT_CORNER_OFFSET }
};
// Counter and state variables for hot corners
std::atomic<int> counter[4] = {};
std::atomic<bool> activated[4] = {};
// Hot corner handling thread
DWORD WINAPI CornerHotFunc(LPVOID lpParam) {
int corner = (int)(intptr_t)lpParam;
CornerAction action;
switch (corner) {
case 0: action = hotCornerConfig.topLeft; break;
case 1: action = hotCornerConfig.topRight; break;
case 2: action = hotCornerConfig.bottomLeft; break;
case 3: action = hotCornerConfig.bottomRight; break;
}
int cnt = counter[corner];
if (action.delay > 0) {
Sleep(action.delay);
}
// Check the counter and execute the action if not interrupted
if (cnt == counter[corner] && !action.content.empty()) {
ExecuteCornerAction(action);
}
return 0;
}
// Mouse hook callback with state and thread management
static LRESULT CALLBACK MouseHookCallback(int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode >= 0 && wParam == WM_MOUSEMOVE) {
MSLLHOOKSTRUCT* evt = (MSLLHOOKSTRUCT*)lParam;
// Check each corner for mouse position
for (int i = 0; i < 4; ++i) {
bool active = PtInRect(&hotCorners[i], evt->pt);
if (active && !activated[i]) {
HANDLE handle = CreateThread(NULL, 0, CornerHotFunc, (LPVOID)(intptr_t)i, 0, NULL);
if (handle != NULL) {
CloseHandle(handle);
}
}
if (!active && activated[i]) {
counter[i] = (counter[i] + 1) % 10000;
}
activated[i] = active;
}
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
int main() {
LoadConfig();
HHOOK mouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookCallback, NULL, 0);
if (!mouseHook) {
std::cerr << "Failed to set mouse hook!" << std::endl;
return 1;
}
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
UnhookWindowsHookEx(mouseHook);
return 0;
}