2020-06-12 03:59:36 +08:00
|
|
|
#include "pch.h"
|
|
|
|
#include "HotkeyManager.h"
|
|
|
|
|
|
|
|
using namespace interop;
|
|
|
|
|
|
|
|
HotkeyManager::HotkeyManager()
|
|
|
|
{
|
2020-07-03 23:21:06 +08:00
|
|
|
keyboardEventCallback = gcnew KeyboardEventCallback(this, &HotkeyManager::KeyboardEventProc);
|
2020-06-12 03:59:36 +08:00
|
|
|
isActiveCallback = gcnew IsActiveCallback(this, &HotkeyManager::IsActiveProc);
|
|
|
|
filterKeyboardCallback = gcnew FilterKeyboardEvent(this, &HotkeyManager::FilterKeyboardProc);
|
|
|
|
|
|
|
|
keyboardHook = gcnew KeyboardHook(
|
|
|
|
keyboardEventCallback,
|
|
|
|
isActiveCallback,
|
2020-07-03 23:21:06 +08:00
|
|
|
filterKeyboardCallback);
|
2020-06-12 03:59:36 +08:00
|
|
|
hotkeys = gcnew Dictionary<HOTKEY_HANDLE, HotkeyCallback ^>();
|
|
|
|
pressedKeys = gcnew Hotkey();
|
|
|
|
keyboardHook->Start();
|
|
|
|
}
|
|
|
|
|
|
|
|
HotkeyManager::~HotkeyManager()
|
|
|
|
{
|
|
|
|
delete keyboardHook;
|
|
|
|
}
|
|
|
|
|
|
|
|
// When all Shortcut keys are pressed, fire the HotkeyCallback event.
|
2020-07-03 23:21:06 +08:00
|
|
|
void HotkeyManager::KeyboardEventProc(KeyboardEvent ^ ev)
|
2020-06-12 03:59:36 +08:00
|
|
|
{
|
2020-07-03 23:21:06 +08:00
|
|
|
// pressedKeys always stores the latest keyboard state
|
2020-06-12 03:59:36 +08:00
|
|
|
auto pressedKeysHandle = GetHotkeyHandle(pressedKeys);
|
|
|
|
if (hotkeys->ContainsKey(pressedKeysHandle))
|
|
|
|
{
|
|
|
|
hotkeys[pressedKeysHandle]->Invoke();
|
2020-07-09 23:14:47 +08:00
|
|
|
|
|
|
|
// After invoking the hotkey send a dummy key to prevent Start Menu from activating
|
|
|
|
INPUT dummyEvent[1] = {};
|
|
|
|
dummyEvent[0].type = INPUT_KEYBOARD;
|
|
|
|
dummyEvent[0].ki.wVk = 0xFF;
|
|
|
|
dummyEvent[0].ki.dwFlags = KEYEVENTF_KEYUP;
|
|
|
|
SendInput(1, dummyEvent, sizeof(INPUT));
|
2020-06-12 03:59:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hotkeys are intended to be global, therefore they are always active no matter the
|
|
|
|
// context in which the keypress occurs.
|
|
|
|
bool HotkeyManager::IsActiveProc()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// KeyboardEvent callback is only fired for relevant key events.
|
2020-07-03 23:21:06 +08:00
|
|
|
bool HotkeyManager::FilterKeyboardProc(KeyboardEvent ^ ev)
|
2020-06-12 03:59:36 +08:00
|
|
|
{
|
2020-07-03 23:21:06 +08:00
|
|
|
// Updating the pressed keys here so we know if the keypress event should be propagated or not.
|
|
|
|
pressedKeys->Win = (GetAsyncKeyState(VK_LWIN) & 0x8000) || (GetAsyncKeyState(VK_RWIN) & 0x8000);
|
|
|
|
pressedKeys->Ctrl = GetAsyncKeyState(VK_CONTROL) & 0x8000;
|
|
|
|
pressedKeys->Alt = GetAsyncKeyState(VK_MENU) & 0x8000;
|
|
|
|
pressedKeys->Shift = GetAsyncKeyState(VK_SHIFT) & 0x8000;
|
|
|
|
pressedKeys->Key = ev->key;
|
2020-06-12 03:59:36 +08:00
|
|
|
|
2020-07-03 23:21:06 +08:00
|
|
|
// Convert to hotkey handle
|
2020-06-12 03:59:36 +08:00
|
|
|
auto pressedKeysHandle = GetHotkeyHandle(pressedKeys);
|
|
|
|
|
2020-07-03 23:21:06 +08:00
|
|
|
// Check if any hotkey matches the pressed keys if the current key event is a key down event
|
|
|
|
if ((ev->message == WM_KEYDOWN || ev->message == WM_SYSKEYDOWN) && hotkeys->ContainsKey(pressedKeysHandle))
|
2020-06-12 03:59:36 +08:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2020-07-03 23:21:06 +08:00
|
|
|
|
2020-06-12 03:59:36 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: Replaces old hotkey if one already present.
|
|
|
|
HOTKEY_HANDLE HotkeyManager::RegisterHotkey(Hotkey ^ hotkey, HotkeyCallback ^ callback)
|
|
|
|
{
|
|
|
|
auto handle = GetHotkeyHandle(hotkey);
|
|
|
|
hotkeys[handle] = callback;
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HotkeyManager::UnregisterHotkey(HOTKEY_HANDLE handle)
|
|
|
|
{
|
|
|
|
hotkeys->Remove(handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
HOTKEY_HANDLE HotkeyManager::GetHotkeyHandle(Hotkey ^ hotkey)
|
|
|
|
{
|
|
|
|
HOTKEY_HANDLE handle = hotkey->Key;
|
|
|
|
handle |= hotkey->Win << 8;
|
|
|
|
handle |= hotkey->Ctrl << 9;
|
|
|
|
handle |= hotkey->Shift << 10;
|
|
|
|
handle |= hotkey->Alt << 11;
|
|
|
|
return handle;
|
|
|
|
}
|