Files
PowerToys/src/modules/fancyzones/FancyZonesLib/KeyboardInput.cpp
Seraphima Zykova 6333e3157e [FancyZones]Use RawInput to detect Shift key, fix locking out Shift key (#32116)
* init RawInputDevice

* static

* handle input

* replace keyboard hooks (ctrl, shift)

* keep ctrl hook

* spellcheck
2024-03-28 16:53:17 +00:00

43 lines
1.2 KiB
C++

#include "pch.h"
#include "KeyboardInput.h"
#include <hidUsage.h>
#include <common/logger/logger.h>
#include <common/utils/winapi_error.h>
bool KeyboardInput::Initialize(HWND window)
{
RAWINPUTDEVICE inputDevice{};
inputDevice.usUsagePage = HID_USAGE_PAGE_GENERIC;
inputDevice.usUsage = HID_USAGE_GENERIC_KEYBOARD;
inputDevice.dwFlags = RIDEV_INPUTSINK;
inputDevice.hwndTarget = window;
bool res = RegisterRawInputDevices(&inputDevice, 1, sizeof(inputDevice));
if (!res)
{
Logger::error(L"RegisterRawInputDevices error: {}", get_last_error_or_default(GetLastError()));
}
return res;
}
std::optional<KeyboardInput::Key> KeyboardInput::OnKeyboardInput(HRAWINPUT hInput)
{
RAWINPUT input;
UINT size = sizeof(input);
auto result = GetRawInputData(hInput, RID_INPUT, &input, &size, sizeof(RAWINPUTHEADER));
if (result < sizeof(RAWINPUTHEADER))
{
return std::nullopt;
}
if (input.header.dwType == RIM_TYPEKEYBOARD)
{
bool pressed = (input.data.keyboard.Flags & RI_KEY_BREAK) == 0;
return KeyboardInput::Key{ input.data.keyboard.VKey, pressed };
}
return std::nullopt;
}