2020-07-10 17:06:01 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "pch.h"
|
2020-08-24 15:19:39 +08:00
|
|
|
#include <functional>
|
2020-10-20 01:49:17 +08:00
|
|
|
#include <common/debug_control.h>
|
2020-07-10 17:06:01 +08:00
|
|
|
|
|
|
|
template<int... keys>
|
|
|
|
class GenericKeyHook
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
GenericKeyHook(std::function<void(bool)> extCallback)
|
|
|
|
{
|
|
|
|
callback = std::move(extCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
void enable()
|
|
|
|
{
|
|
|
|
#if defined(DISABLE_LOWLEVEL_HOOKS_WHEN_DEBUGGED)
|
|
|
|
if (IsDebuggerPresent())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (!hHook)
|
|
|
|
{
|
|
|
|
hHook = SetWindowsHookEx(WH_KEYBOARD_LL, GenericKeyHookProc, GetModuleHandle(NULL), 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void disable()
|
|
|
|
{
|
|
|
|
if (hHook)
|
|
|
|
{
|
|
|
|
UnhookWindowsHookEx(hHook);
|
|
|
|
hHook = NULL;
|
|
|
|
callback(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-12-30 00:20:12 +08:00
|
|
|
inline static HHOOK hHook = nullptr;
|
2020-08-24 15:19:39 +08:00
|
|
|
inline static std::function<void(bool)> callback;
|
2020-07-10 17:06:01 +08:00
|
|
|
|
|
|
|
static LRESULT CALLBACK GenericKeyHookProc(int nCode, WPARAM wParam, LPARAM lParam)
|
|
|
|
{
|
|
|
|
if (nCode == HC_ACTION)
|
|
|
|
{
|
|
|
|
if (wParam == WM_KEYDOWN || wParam == WM_KEYUP)
|
|
|
|
{
|
|
|
|
PKBDLLHOOKSTRUCT kbdHookStruct = (PKBDLLHOOKSTRUCT)lParam;
|
|
|
|
if (((kbdHookStruct->vkCode == keys) || ...))
|
|
|
|
{
|
|
|
|
callback(wParam == WM_KEYDOWN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return CallNextHookEx(hHook, nCode, wParam, lParam);
|
|
|
|
}
|
|
|
|
};
|