MCPcopy Create free account
hub / github.com/AutoHotkey/AutoHotkey / HookThreadProc

Function HookThreadProc

source/hook.cpp:4243–4332  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4241
4242
4243DWORD WINAPI HookThreadProc(LPVOID aUnused)
4244// The creator of this thread relies on the fact that this function always exits its thread
4245// when both hooks are deactivated.
4246{
4247 MSG msg;
4248 bool problem_activating_hooks;
4249
4250 for (;;) // Infinite loop for pumping messages in this thread. This thread will exit via any use of "return" below.
4251 {
4252 if (GetMessage(&msg, NULL, 0, 0) == -1) // -1 is an error, 0 means WM_QUIT.
4253 continue; // Probably happens only when bad parameters are passed to GetMessage().
4254
4255 switch (msg.message)
4256 {
4257 case WM_QUIT:
4258 // After this message, fall through to the next case below so that the hooks will be removed before
4259 // exiting this thread.
4260 msg.wParam = 0; // Indicate to AHK_CHANGE_HOOK_STATE that both hooks should be deactivated.
4261 // ********
4262 // NO BREAK IN ABOVE, FALL INTO NEXT CASE:
4263 // ********
4264 case AHK_CHANGE_HOOK_STATE: // No blank line between this in the above to indicate fall-through.
4265 // In this case, wParam contains the bitwise set of hooks that should be active.
4266 problem_activating_hooks = false;
4267 if (msg.wParam & HOOK_KEYBD) // Activate the keyboard hook (if it isn't already).
4268 {
4269 if (!g_KeybdHook)
4270 {
4271 // v1.0.39: Reset *before* hook is installed to avoid any chance that events can
4272 // flow into the hook prior to the reset:
4273 if (msg.lParam) // Sender of msg. is signaling that reset should be done.
4274 ResetHook(false, HOOK_KEYBD, true);
4275 if ( !(g_KeybdHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeybdProc, g_hInstance, 0)) )
4276 problem_activating_hooks = true;
4277 }
4278 }
4279 else // Caller specified that the keyboard hook is to be deactivated (if it isn't already).
4280 if (g_KeybdHook)
4281 if (UnhookWindowsHookEx(g_KeybdHook))
4282 g_KeybdHook = NULL;
4283
4284 if (msg.wParam & HOOK_MOUSE) // Activate the mouse hook (if it isn't already).
4285 {
4286 if (!g_MouseHook)
4287 {
4288 if (msg.lParam) // Sender of msg. is signaling that reset should be done.
4289 ResetHook(false, HOOK_MOUSE, true);
4290 if ( !(g_MouseHook = SetWindowsHookEx(WH_MOUSE_LL, LowLevelMouseProc, g_hInstance, 0)) )
4291 problem_activating_hooks = true;
4292 }
4293 }
4294 else // Caller specified that the mouse hook is to be deactivated (if it isn't already).
4295 if (g_MouseHook)
4296 if (UnhookWindowsHookEx(g_MouseHook))
4297 g_MouseHook = NULL;
4298
4299 // Upon failure, don't display MsgBox here because although MsgBox's own message pump would
4300 // service the hook that didn't fail (if it's active), it's best to avoid any blocking calls

Callers

nothing calls this directly

Calls 2

ResetHookFunction · 0.85
SetKeyHistoryMaxFunction · 0.85

Tested by

no test coverage detected