-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathInputHooks.cpp
267 lines (214 loc) · 9.62 KB
/
InputHooks.cpp
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include "OverlayApplication.h"
#include "MumbleLink.h"
#include "InputHooks.h"
#include "MarkerEditor.h"
bool ShiftState = false;
bool HooksInitialized = false;
DWORD hookThreadID = 0;
bool disableHooks = false;
extern HWND gameWindow;
extern CWBApplication* App;
TBOOL keyboardHookActive = false;
TBOOL mouseHookActive = false;
LRESULT __stdcall MyKeyboardProc( int ccode, WPARAM wParam, LPARAM lParam )
{
if ( disableHooks )
return CallNextHookEx( 0, ccode, wParam, lParam );
if ( ccode == HC_ACTION )
{
KBDLLHOOKSTRUCT* pkbdllhook = (KBDLLHOOKSTRUCT*)lParam;
HKL dwhkl = 0;
BYTE dbKbdState[ 256 ];
TCHAR szCharBuf[ 32 ];
static KBDLLHOOKSTRUCT lastState = { 0 };
GetKeyboardState( dbKbdState );
dwhkl = GetKeyboardLayout( GetWindowThreadProcessId( GetForegroundWindow(), NULL ) );
if ( ToAsciiEx( pkbdllhook->vkCode, pkbdllhook->scanCode, dbKbdState, (LPWORD)szCharBuf, 0, dwhkl ) == -1 )
{
//PostMessage( (HWND)App->GetHandle(), WM_DEADCHAR, pkbdllhook->vkCode, 1 | ( pkbdllhook->scanCode << 16 ) + ( pkbdllhook->flags << 24 ) );
//Save the current keyboard state.
lastState = *pkbdllhook;
//You might also need to hang onto the dbKbdState array... I'm thinking not.
//Clear out the buffer to return to the previous state - wait for ToAsciiEx to return a value other than -1 by passing the same key again. It should happen after 1 call.
while ( ToAsciiEx( pkbdllhook->vkCode, pkbdllhook->scanCode, dbKbdState, (LPWORD)szCharBuf, 0, dwhkl ) < 0 );
}
else
{
//for ( int x = 0; x < 32; x++ )
// if ( szCharBuf[ x ] )
// {
//PostMessage( (HWND)App->GetHandle(), WM_CHAR, szCharBuf[ 0 ], 1 | ( pkbdllhook->scanCode << 16 ) + ( pkbdllhook->flags << 24 ) );
//}
//else break;
//Do something with szCharBuf here since this will overwrite it...
//If we have a saved vkCode from last call, it was a dead key we need to place back in the buffer.
if ( lastState.vkCode != 0 )
{
//Safest to just clear this.
memset( dbKbdState, 0, 256 );
//Put the old vkCode back into the locale's buffer.
ToAsciiEx( lastState.vkCode, lastState.scanCode, dbKbdState, (LPWORD)szCharBuf, 0, dwhkl );
//Set vkCode to 0, we can use this as a flag as a vkCode of 0 is invalid.
lastState.vkCode = 0;
}
}
}
if ( ccode < 0 )
return CallNextHookEx( 0, ccode, wParam, lParam );
if ( wParam != WM_KEYDOWN && wParam != WM_KEYUP && wParam != WM_CHAR && wParam != WM_DEADCHAR && wParam != WM_UNICHAR )
return CallNextHookEx( 0, ccode, wParam, lParam );
auto wnd = GetForegroundWindow();
if ( ccode != HC_ACTION || !lParam || ( wnd != gameWindow && App && wnd != (HWND)App->GetHandle() ) )
return CallNextHookEx( 0, ccode, wParam, lParam );
if ( App && wnd == (HWND)App->GetHandle() )
return CallNextHookEx( 0, ccode, wParam, lParam );
KBDLLHOOKSTRUCT* kbdat = (KBDLLHOOKSTRUCT*)lParam;
PostMessage( (HWND)App->GetHandle(), wParam, kbdat->vkCode, 1 | ( kbdat->scanCode << 16 ) + ( kbdat->flags << 24 ) );
return ( CallNextHookEx( 0, ccode, wParam, lParam ) );
}
LRESULT __stdcall KeyboardHook( int code, WPARAM wParam, LPARAM lParam )
{
if ( disableHooks || mumbleLink.textboxHasFocus )
return CallNextHookEx( 0, code, wParam, lParam );
// !!!!!!!!!!!!! https://stackoverflow.com/questions/3548932/keyboard-hook-changes-the-behavior-of-keys
if ( code < 0 )
return CallNextHookEx( 0, code, wParam, lParam );
if ( wParam != WM_KEYDOWN && wParam != WM_KEYUP && wParam != WM_CHAR && wParam != WM_DEADCHAR && wParam != WM_UNICHAR )
return CallNextHookEx( 0, code, wParam, lParam );
auto wnd = GetForegroundWindow();
if ( code != HC_ACTION || !lParam || ( wnd != gameWindow && App && wnd != (HWND)App->GetHandle() ) )
return CallNextHookEx( 0, code, wParam, lParam );
if ( App && wnd == (HWND)App->GetHandle() )
return CallNextHookEx( 0, code, wParam, lParam );
KBDLLHOOKSTRUCT* kbdat = (KBDLLHOOKSTRUCT*)lParam;
UINT mapped = MapVirtualKey( kbdat->vkCode, MAPVK_VK_TO_CHAR );
//App->InjectMessage( wParam, kbdat->vkCode, 1 | ( kbdat->scanCode << 16 ) + ( kbdat->flags << 24 ) );
bool inFocus = App->GetFocusItem() && App->GetFocusItem()->InstanceOf( "textbox" );
//if ( !( mapped & ( 1 << 31 ) ) && !inFocus && wParam == WM_KEYDOWN )
// App->InjectMessage( WM_CHAR, mapped, 0 );
if ( mapped & ( 1 << 31 ) && !inFocus )
return CallNextHookEx( 0, 0, wParam, (LPARAM)lParam );
if ( !inFocus )
{
if ( wParam == WM_KEYDOWN )
{
//(*(void(__thiscall**)(_DWORD*, signed int, UINT, _DWORD))(*app + 68))(app, 258, mapped, 0);
App->InjectMessage( WM_CHAR, mapped, 0 );
return CallNextHookEx( 0, 0, WM_KEYDOWN, (LPARAM)lParam );
}
return CallNextHookEx( 0, 0, wParam, (LPARAM)lParam );
}
PostMessage( (HWND)App->GetHandle(), wParam, kbdat->vkCode, 1 | ( kbdat->scanCode << 16 ) + ( kbdat->flags << 24 ) );
return 1;
//if ( inFocus )
// return 1;
//FILE *f = fopen( "keylog.txt", "at" );
//fprintf( f, "keyboard hook: %d %x %d %d %d %x\n", wParam, kbdat->flags, kbdat->scanCode, kbdat->time, kbdat->vkCode, kbdat->dwExtraInfo );
//fclose( f );
// if (wParam == WM_KEYDOWN /*|| wParam == WM_SYSKEYDOWN*/)
// {
// if (kbdat->vkCode == VK_SHIFT || kbdat->vkCode == VK_LSHIFT || kbdat->vkCode == VK_RSHIFT)
// ShiftState = true;
// WORD charcode;
// unsigned char state[256];
// GetKeyboardState(state);
// state[0x10] = 0x80 * ShiftState;
// int len = ToAscii(kbdat->vkCode, kbdat->scanCode, state, &charcode, 0);
// if (len > 0)
// {
// PostMessage((HWND)App->GetHandle(), (wParam == WM_KEYDOWN) ? WM_CHAR : WM_SYSCHAR, charcode, 0);
// }
// }
// if (wParam == WM_KEYUP /*|| wParam == WM_SYSKEYUP*/)
// {
// if (kbdat->vkCode == VK_SHIFT || kbdat->vkCode == VK_LSHIFT || kbdat->vkCode == VK_RSHIFT)
// ShiftState = false;
// }
//// if (kbdat->vkCode != VK_MENU && kbdat->vkCode != VK_LMENU && kbdat->vkCode != VK_RMENU)
//if ( ( wParam == WM_KEYUP || wParam == WM_KEYDOWN || wParam == WM_CHAR ) && App->GetFocusItem()->InstanceOf( "textbox" ) )
// return 1;
//return CallNextHookEx( 0, code, wParam, lParam );
}
LRESULT __stdcall MouseHook( int code, WPARAM wParam, LPARAM lParam )
{
if ( disableHooks )
return CallNextHookEx( 0, code, wParam, lParam );
auto wnd = GetForegroundWindow();
if ( code < 0 || !lParam || ( wnd != gameWindow && App && wnd != (HWND)App->GetHandle() ) )
return CallNextHookEx( 0, code, wParam, lParam );
MSLLHOOKSTRUCT* mousedat = (MSLLHOOKSTRUCT*)lParam;
POINT ap = mousedat->pt;
//SetCursorPos(ap.x, ap.y);
ScreenToClient( (HWND)App->GetHandle(), &ap );
if ( wParam == WM_LBUTTONDOWN || wParam == WM_RBUTTONDOWN || wParam == WM_LBUTTONDBLCLK )
{
PostMessage( (HWND)App->GetHandle(), wParam, 0, ap.x + ( ap.y << 16 ) );
auto item = App->GetItemUnderMouse( CPoint( ap.x, ap.y ), wParam == WM_LBUTTONDOWN ? WBM_LEFTBUTTONDOWN : WBM_RIGHTBUTTONDOWN );
auto markerEditor = App->GetRoot()->FindChildByID<GW2MarkerEditor>( "MarkerEditor" );
if ( markerEditor && !markerEditor->IsHidden() )
{
if ( markerEditor->ShouldPassMouseEvent() )
return CallNextHookEx( 0, code, wParam, lParam );
}
if ( item && item != App->GetRoot() && item->GetType() != "clickthroughbutton" )
return 1;
}
if ( wParam == WM_LBUTTONUP || wParam == WM_RBUTTONUP || wParam == WM_MOUSEMOVE )
{
PostMessage( (HWND)App->GetHandle(), wParam, 0, ap.x + ( ap.y << 16 ) );
return CallNextHookEx( 0, code, wParam, lParam ); //let these through so we don't mess up dragging etc
}
return CallNextHookEx( 0, code, wParam, lParam );
}
void InitInputHooks()
{
if ( HooksInitialized )
return;
if ( IsDebuggerPresent() )
return;
auto hookThread = CreateThread( NULL, 0,
[]( LPVOID data )
{
auto keyboardHook = SetWindowsHookEx( WH_KEYBOARD_LL, KeyboardHook, NULL, 0 );
auto mouseHook = SetWindowsHookEx( WH_MOUSE_LL, MouseHook, NULL, 0 );
MSG msg;
keyboardHookActive = true;
mouseHookActive = true;
while ( GetMessage( &msg, 0, 0, 0 ) > 0 )
{
if ( msg.message == WM_QUIT )
break;
TranslateMessage( &msg );
}
DispatchMessage( &msg );
UnhookWindowsHookEx( keyboardHook );
UnhookWindowsHookEx( mouseHook );
keyboardHookActive = false;
mouseHookActive = false;
return (DWORD)0;
},
0, 0, &hookThreadID );
HooksInitialized = true;
}
void ShutDownInputHooks()
{
if ( hookThreadID )
PostThreadMessage( hookThreadID, WM_QUIT, 0, 0 );
while ( keyboardHookActive );
while ( mouseHookActive );
}
int GetTacOKeyCode( int key )
{
return key + App->GetAltState() * TacO_ALT + App->GetCtrlState() * TacO_CTRL + App->GetShiftState() * TacO_SHIFT;
}
CString GetTacOKeyString( int key )
{
CString result;
if ( key & TacO_SHIFT )
result += "SHIFT+";
if ( key & TacO_CTRL )
result += "CTRL+";
if ( key & TacO_ALT )
result += "ALT+";
return result + CString::Format( "%c", key & 0xfffff );
}