forked from Ilyaki/ProtoInput
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFocusHook.cpp
More file actions
150 lines (127 loc) · 4.48 KB
/
Copy pathFocusHook.cpp
File metadata and controls
150 lines (127 loc) · 4.48 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
#include "FocusHook.h"
#include <imgui.h>
#include "HwndSelector.h"
#include "INISettings.h"
namespace Proto
{
inline HWND GetHwnd()
{
if (Proto::fixWindowFocus)
{
HWND current = (HWND)HwndSelector::GetSelectedHwnd();
// Check if the handle is 0 OR if the window no longer exists (e.g. after resolution change)
if (current == 0 || !IsWindow(current))
{
HwndSelector::UpdateMainHwnd();
current = (HWND)HwndSelector::GetSelectedHwnd();
}
return current;
}
else // Default
{
if (HwndSelector::GetSelectedHwnd() == 0)
HwndSelector::UpdateMainHwnd();
return (HWND)HwndSelector::GetSelectedHwnd();
}
}
HWND WINAPI Hook_GetForegroundWindow()
{
return GetHwnd();
}
HWND WINAPI Hook_WindowFromPoint(POINT Point)
{
return GetHwnd();
}
HWND WINAPI Hook_GetActiveWindow()
{
return GetHwnd();
}
BOOL WINAPI Hook_IsWindowEnabled(HWND hWnd)
{
return TRUE;
}
HWND WINAPI Hook_GetFocus()
{
return GetHwnd();
}
HWND WINAPI Hook_GetCapture()
{
return GetHwnd();
}
HWND WINAPI Hook_SetCapture(HWND inputHwnd)
{
return inputHwnd;
}
BOOL WINAPI Hook_ReleaseCapture()
{
return TRUE;
}
HWND WINAPI Hook_SetActiveWindow(HWND input)
{
return input;
}
HWND WINAPI Hook_SetFocus(HWND input)
{
return input;
}
BOOL WINAPI Hook_SetForegroundWindow(HWND hWnd)
{
return true;
}
void FocusHook::ShowGuiStatus()
{
if (IsInstalled() && needReinstalling)
{
ImGui::PushID(1234);
ImGui::PushStyleColor(ImGuiCol_Text,
(ImVec4)ImColor::HSV(35.0f / 255.0f, 0.9f, 0.9f)
);
ImGui::TextWrapped("Disable and enable the hook for changes to take effect");
ImGui::PopStyleColor(1);
ImGui::PopID();
}
ImGui::TextWrapped("Window directing fake focus to: %d (0x%X)", HwndSelector::GetSelectedHwnd(), HwndSelector::GetSelectedHwnd());
if (ImGui::Button("Find new main window"))
HwndSelector::UpdateMainHwnd();
HookCheckbox("GetForegroundWindow", &enabledHookGetForegroundWindow);
HookCheckbox("WindowFromPoint", &enabledHookWindowFromPoint);
HookCheckbox("GetActiveWindow", &enabledHookGetActiveWindow);
HookCheckbox("IsWindowEnabled", &enabledHookIsWindowEnabled);
HookCheckbox("GetFocus", &enabledHookGetFocus);
HookCheckbox("GetCapture", &enabledHookGetCapture);
HookCheckbox("SetCapture", &enabledHookSetCapture);
HookCheckbox("SetActiveWindow", &enabledHookSetActiveWindow);
HookCheckbox("ReleaseCapture", &enabledHookReleaseCapture);
HookCheckbox("SetFocus", &enabledHookSetFocus);
HookCheckbox("SetForegroundWindow", &enabledHookSetForegroundWindow);
}
void FocusHook::InstallImpl()
{
needReinstalling = false;
if (enabledHookGetForegroundWindow) hookInfoGetForegroundWindow = std::get<1>(InstallNamedHook(L"user32", "GetForegroundWindow", Hook_GetForegroundWindow));
if (enabledHookWindowFromPoint) hookInfoWindowFromPoint = std::get<1>(InstallNamedHook(L"user32", "WindowFromPoint", Hook_WindowFromPoint));
if (enabledHookGetActiveWindow) hookInfoGetActiveWindow = std::get<1>(InstallNamedHook(L"user32", "GetActiveWindow", Hook_GetActiveWindow));
if (enabledHookIsWindowEnabled) hookInfoIsWindowEnabled = std::get<1>(InstallNamedHook(L"user32", "IsWindowEnabled", Hook_IsWindowEnabled));
if (enabledHookGetFocus) hookInfoGetFocus = std::get<1>(InstallNamedHook(L"user32", "GetFocus", Hook_GetFocus));
if (enabledHookGetCapture) hookInfoGetCapture = std::get<1>(InstallNamedHook(L"user32", "GetCapture", Hook_GetCapture));
if (enabledHookSetCapture) hookInfoSetCapture = std::get<1>(InstallNamedHook(L"user32", "SetCapture", Hook_SetCapture));
if (enabledHookReleaseCapture) hookInfoReleaseCapture = std::get<1>(InstallNamedHook(L"user32", "ReleaseCapture", Hook_ReleaseCapture));
if (enabledHookSetActiveWindow) hookInfoSetActiveWindow = std::get<1>(InstallNamedHook(L"user32", "SetActiveWindow", Hook_SetActiveWindow));
if (enabledHookSetFocus) hookInfoSetFocus = std::get<1>(InstallNamedHook(L"user32", "SetFocus", Hook_SetFocus));
if (enabledHookSetForegroundWindow) hookInfoSetForegroundWindow = std::get<1>(InstallNamedHook(L"user32", "SetForegroundWindow", Hook_SetForegroundWindow));
}
void FocusHook::UninstallImpl()
{
UninstallHook(&hookInfoGetForegroundWindow);
UninstallHook(&hookInfoWindowFromPoint );
UninstallHook(&hookInfoGetActiveWindow );
UninstallHook(&hookInfoIsWindowEnabled );
UninstallHook(&hookInfoGetFocus );
UninstallHook(&hookInfoGetCapture );
UninstallHook(&hookInfoSetCapture );
UninstallHook(&hookInfoReleaseCapture );
UninstallHook(&hookInfoSetActiveWindow );
UninstallHook(&hookInfoSetFocus );
UninstallHook(&hookInfoSetForegroundWindow);
}
}