-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCtrlClick.cpp
More file actions
81 lines (71 loc) · 2.07 KB
/
CtrlClick.cpp
File metadata and controls
81 lines (71 loc) · 2.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
#include "windows.h"
HHOOK hook;
MSLLHOOKSTRUCT msStruct;
char windowClass[256];
char windowText[256];
LRESULT __stdcall HookCallback(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode >= 0 &&
(wParam == WM_LBUTTONDOWN ||
wParam == WM_LBUTTONUP))
{
const auto ctrl = (GetKeyState(VK_CONTROL) & 0x8000) != 0;
if (ctrl)
{
msStruct = *reinterpret_cast<MSLLHOOKSTRUCT*>(lParam);
const auto hWnd = WindowFromPoint(msStruct.pt);
if (hWnd &&
GetClassNameA(hWnd, windowClass, 255) &&
GetWindowTextA(hWnd, windowText, 255) &&
(strcmp(windowClass, "Scintilla") == 0 || // Notepad++, SciTE, etc.
strcmp(windowClass, "Chrome_RenderWidgetHostHWND") == 0 && strcmp(windowText, "Chrome Legacy Window") == 0)) // VS Code, maybe others?
{
if (wParam == WM_LBUTTONDOWN)
{
// release CTRL
INPUT input;
input.type = INPUT_KEYBOARD;
input.ki.wVk = VK_CONTROL;
input.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &input, sizeof(input));
// click + down in place
POINT client;
client.x = msStruct.pt.x;
client.y = msStruct.pt.y;
ScreenToClient(hWnd, &client);
const auto mouseLParam = MAKELPARAM(client.x, client.y);
SendMessage(hWnd, WM_LBUTTONDOWN, 0, mouseLParam);
SendMessage(hWnd, WM_LBUTTONUP, 0, mouseLParam);
SendMessage(hWnd, WM_LBUTTONDOWN, 0, mouseLParam);
// press CTRL
input.ki.dwFlags = 0;
SendInput(1, &input, sizeof(input));
return 1; // don't allow this current mouse down to be processed
}
}
}
}
return CallNextHookEx(hook, nCode, wParam, lParam);
}
void SetHook()
{
if (!(hook = SetWindowsHookEx(WH_MOUSE_LL, HookCallback, GetModuleHandle(NULL), 0)))
{
MessageBoxA(NULL, "Failed to install Ctrl+Click hook!", "Error", MB_ICONERROR);
}
}
void ReleaseHook()
{
UnhookWindowsHookEx(hook);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow)
{
SetHook();
MSG message;
while (GetMessage(&message, NULL, 0, 0) != 0) {
TranslateMessage(&message);
DispatchMessage(&message);
}
ReleaseHook();
return 0;
}