From 2207c3f4f8b27218b1d9ce3a7c37a7d0e99c678e Mon Sep 17 00:00:00 2001 From: Michael Richters Date: Sat, 24 Oct 2020 17:50:59 -0500 Subject: [PATCH] Add modifier information to KeyboardReport class This change adds the keycodes for keyboard modifier keys to the vector returned by KeyboardReport::ActiveKeycodes(). Also, a simple method for getting the modifiers byte from a keyboard hid report. Signed-off-by: Michael Richters --- testing/KeyboardReport.cpp | 33 +++++++++++++++++++++++++++++---- testing/KeyboardReport.h | 2 ++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/testing/KeyboardReport.cpp b/testing/KeyboardReport.cpp index 8b290eae..31e87de3 100644 --- a/testing/KeyboardReport.cpp +++ b/testing/KeyboardReport.cpp @@ -28,13 +28,38 @@ KeyboardReport::KeyboardReport(const void* data) { } std::vector KeyboardReport::ActiveKeycodes() const { - std::vector active_keys; + auto keycodes = ActiveNonModifierKeycodes(); + auto mods = ActiveModifierKeycodes(); + keycodes.insert(keycodes.end(), mods.begin(), mods.end()); + return keycodes; +} + +std::vector KeyboardReport::ActiveModifierKeycodes() const { + constexpr uint8_t modifier_keycode_offset{HID_KEYBOARD_FIRST_MODIFIER}; + + std::vector active_modifiers; + + uint8_t modifiers{report_data_.modifiers}, mask{1}; + + for (uint8_t i{0}; modifiers != 0; ++i, modifiers >>= 1) { + if (modifiers & mask) { + active_modifiers.push_back(i + modifier_keycode_offset); + } + } + + return active_modifiers; +} + +std::vector KeyboardReport::ActiveNonModifierKeycodes() const { + std::vector active_keycodes; + for (uint8_t i = 0; i < HID_LAST_KEY; ++i) { uint8_t bit = 1 << (uint8_t(i) % 8); - uint8_t key_code = report_data_.keys[i / 8] & bit; - if (key_code) active_keys.push_back(i); + uint8_t keycode = report_data_.keys[i / 8] & bit; + if (keycode) active_keycodes.push_back(i); } - return active_keys; + + return active_keycodes; } } // namespace testing diff --git a/testing/KeyboardReport.h b/testing/KeyboardReport.h index a17ab1d0..3654d6c4 100644 --- a/testing/KeyboardReport.h +++ b/testing/KeyboardReport.h @@ -34,6 +34,8 @@ class KeyboardReport { KeyboardReport(const void* data); std::vector ActiveKeycodes() const; + std::vector ActiveModifierKeycodes() const; + std::vector ActiveNonModifierKeycodes() const; private: ReportData report_data_;