2020-06-11 12:59:36 -07:00
|
|
|
#include "pch.h"
|
|
|
|
|
#include "HotkeyManager.h"
|
2024-08-08 15:26:43 +01:00
|
|
|
#include "HotkeyManager.g.cpp"
|
2020-06-11 12:59:36 -07:00
|
|
|
|
2024-08-08 15:26:43 +01:00
|
|
|
namespace winrt::PowerToys::Interop::implementation
|
2020-06-11 12:59:36 -07:00
|
|
|
{
|
2024-08-08 15:26:43 +01:00
|
|
|
HotkeyManager::HotkeyManager()
|
|
|
|
|
{
|
|
|
|
|
keyboardEventCallback = KeyboardEventCallback{ this, &HotkeyManager::KeyboardEventProc };
|
|
|
|
|
isActiveCallback = IsActiveCallback{ this, &HotkeyManager::IsActiveProc };
|
|
|
|
|
filterKeyboardCallback = FilterKeyboardEvent{ this, &HotkeyManager::FilterKeyboardProc };
|
|
|
|
|
keyboardHook = KeyboardHook{ keyboardEventCallback, isActiveCallback, filterKeyboardCallback };
|
|
|
|
|
keyboardHook.Start();
|
|
|
|
|
}
|
2020-06-11 12:59:36 -07:00
|
|
|
|
2024-08-08 15:26:43 +01:00
|
|
|
// When all Shortcut keys are pressed, fire the HotkeyCallback event.
|
|
|
|
|
void HotkeyManager::KeyboardEventProc(KeyboardEvent ev)
|
|
|
|
|
{
|
|
|
|
|
// pressedKeys always stores the latest keyboard state
|
|
|
|
|
auto pressedKeysHandle = GetHotkeyHandle(pressedKeys);
|
|
|
|
|
if (hotkeys.find(pressedKeysHandle) != hotkeys.end())
|
|
|
|
|
{
|
|
|
|
|
hotkeys[pressedKeysHandle]();
|
2020-06-11 12:59:36 -07:00
|
|
|
|
2024-08-08 15:26:43 +01: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-11 12:59:36 -07:00
|
|
|
|
2024-08-08 15:26:43 +01:00
|
|
|
// Hotkeys are intended to be global, therefore they are always active no matter the
|
|
|
|
|
// context in which the keypress occurs.
|
|
|
|
|
bool HotkeyManager::IsActiveProc()
|
2020-06-11 12:59:36 -07:00
|
|
|
{
|
2024-08-08 15:26:43 +01:00
|
|
|
return true;
|
2020-06-11 12:59:36 -07:00
|
|
|
}
|
2024-08-08 15:26:43 +01:00
|
|
|
bool HotkeyManager::FilterKeyboardProc(KeyboardEvent ev)
|
|
|
|
|
{
|
|
|
|
|
// 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 = static_cast<unsigned char>(ev.key);
|
2020-06-11 12:59:36 -07:00
|
|
|
|
2024-08-08 15:26:43 +01:00
|
|
|
// Convert to hotkey handle
|
|
|
|
|
auto pressedKeysHandle = GetHotkeyHandle(pressedKeys);
|
2020-06-11 12:59:36 -07:00
|
|
|
|
2024-08-08 15:26:43 +01: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.find(pressedKeysHandle)!=hotkeys.end())
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-06-11 12:59:36 -07:00
|
|
|
|
2024-08-08 15:26:43 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
2020-06-11 12:59:36 -07:00
|
|
|
|
2024-08-08 15:26:43 +01:00
|
|
|
uint16_t HotkeyManager::RegisterHotkey(winrt::PowerToys::Interop::Hotkey const& _hotkey, winrt::PowerToys::Interop::HotkeyCallback const& _callback)
|
2020-06-11 12:59:36 -07:00
|
|
|
{
|
2024-08-08 15:26:43 +01:00
|
|
|
auto handle = GetHotkeyHandle(_hotkey);
|
|
|
|
|
hotkeys[handle] = _callback;
|
|
|
|
|
return handle;
|
2020-06-11 12:59:36 -07:00
|
|
|
}
|
2020-07-03 08:21:06 -07:00
|
|
|
|
2024-08-08 15:26:43 +01:00
|
|
|
void HotkeyManager::UnregisterHotkey(uint16_t _handle)
|
|
|
|
|
{
|
|
|
|
|
auto iter = hotkeys.find(_handle);
|
|
|
|
|
if (iter != hotkeys.end()) {
|
|
|
|
|
hotkeys.erase(iter);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-11 12:59:36 -07:00
|
|
|
|
2024-08-08 15:26:43 +01:00
|
|
|
void HotkeyManager::Close()
|
|
|
|
|
{
|
|
|
|
|
}
|
2020-06-11 12:59:36 -07:00
|
|
|
|
2024-08-08 15:26:43 +01:00
|
|
|
uint16_t HotkeyManager::GetHotkeyHandle(Hotkey hotkey)
|
|
|
|
|
{
|
|
|
|
|
uint16_t handle = hotkey.Key;
|
|
|
|
|
handle |= hotkey.Win << 8;
|
|
|
|
|
handle |= hotkey.Ctrl << 9;
|
|
|
|
|
handle |= hotkey.Shift << 10;
|
|
|
|
|
handle |= hotkey.Alt << 11;
|
|
|
|
|
return handle;
|
|
|
|
|
}
|
2020-06-11 12:59:36 -07:00
|
|
|
}
|