mirror of
https://github.com/microsoft/PowerToys.git
synced 2024-12-26 18:58:29 +08:00
60 lines
1.3 KiB
C++
60 lines
1.3 KiB
C++
#pragma once
|
|
|
|
#include "pch.h"
|
|
#include <functional>
|
|
|
|
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:
|
|
inline static HHOOK hHook;
|
|
inline static std::function<void(bool)> callback;
|
|
|
|
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);
|
|
}
|
|
};
|