From d1594f9529a99f56d7ed3fb4267efe3b893c9ce0 Mon Sep 17 00:00:00 2001 From: Michael Maltese Date: Tue, 9 Aug 2016 19:54:59 -0700 Subject: [PATCH] use a std::map instead of running through the AOS --- .../Quartz/QuartzKeyboardAndMouse.h | 1 + .../Quartz/QuartzKeyboardAndMouse.mm | 39 +++++++++---------- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/Source/Core/InputCommon/ControllerInterface/Quartz/QuartzKeyboardAndMouse.h b/Source/Core/InputCommon/ControllerInterface/Quartz/QuartzKeyboardAndMouse.h index 0f5eafc895..3dd352a003 100644 --- a/Source/Core/InputCommon/ControllerInterface/Quartz/QuartzKeyboardAndMouse.h +++ b/Source/Core/InputCommon/ControllerInterface/Quartz/QuartzKeyboardAndMouse.h @@ -24,6 +24,7 @@ private: private: CGKeyCode m_keycode; + std::string m_name; }; class Cursor : public Input diff --git a/Source/Core/InputCommon/ControllerInterface/Quartz/QuartzKeyboardAndMouse.mm b/Source/Core/InputCommon/ControllerInterface/Quartz/QuartzKeyboardAndMouse.mm index dea495feb9..6827353913 100644 --- a/Source/Core/InputCommon/ControllerInterface/Quartz/QuartzKeyboardAndMouse.mm +++ b/Source/Core/InputCommon/ControllerInterface/Quartz/QuartzKeyboardAndMouse.mm @@ -13,20 +13,7 @@ namespace Quartz { KeyboardAndMouse::Key::Key(CGKeyCode keycode) : m_keycode(keycode) { -} - -ControlState KeyboardAndMouse::Key::GetState() const -{ - return CGEventSourceKeyState(kCGEventSourceStateHIDSystemState, m_keycode) != 0; -} - -std::string KeyboardAndMouse::Key::GetName() const -{ - static const struct - { - const CGKeyCode code; - const char* const name; - } named_keys[] = { + static const std::map named_keys = { {kVK_ANSI_A, "A"}, {kVK_ANSI_B, "B"}, {kVK_ANSI_C, "C"}, @@ -127,18 +114,28 @@ std::string KeyboardAndMouse::Key::GetName() const {kVK_RightOption, "Right Alt"}, }; - for (auto& named_key : named_keys) - if (named_key.code == m_keycode) - return named_key.name; + if (named_keys.find(m_keycode) != named_keys.end()) + m_name = named_keys.at(m_keycode); + else + m_name = "Key " + std::to_string(m_keycode); +} - return "Key " + std::to_string(m_keycode); +ControlState KeyboardAndMouse::Key::GetState() const +{ + return CGEventSourceKeyState(kCGEventSourceStateHIDSystemState, m_keycode) != 0; +} + +std::string KeyboardAndMouse::Key::GetName() const +{ + return m_name; } KeyboardAndMouse::KeyboardAndMouse(void* window) { - // All ANSI keycodes in are 0x7e or lower - for (int i = 0; i < 0x80; i++) - AddInput(new Key(i)); + // All keycodes in are 0x7e or lower. If you notice + // keys that aren't being recognized, bump this number up! + for (int keycode = 0; keycode < 0x80; ++keycode) + AddInput(new Key(keycode)); m_windowid = [[reinterpret_cast(window) window] windowNumber];