Use lazy loading for keyboard scancodes on macos

This commit is contained in:
Lorenzooone 2024-09-14 22:05:30 +02:00 committed by Chris Thrasher
parent 20b048fb08
commit 6bcc3414fc
2 changed files with 17 additions and 6 deletions

View File

@ -286,7 +286,8 @@ private:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// Member data // Member data
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
IOHIDManagerRef m_manager{}; ///< Underlying HID Manager IOHIDManagerRef m_manager{}; ///< Underlying HID Manager
bool m_keysInitialized{}; ///< Has initializeKeyboard been called at least once?
EnumArray<Keyboard::Scancode, IOHIDElements, Keyboard::ScancodeCount> m_keys; ///< All the keys on any connected keyboard EnumArray<Keyboard::Scancode, IOHIDElements, Keyboard::ScancodeCount> m_keys; ///< All the keys on any connected keyboard
EnumArray<Keyboard::Key, Keyboard::Scancode, Keyboard::KeyCount> m_keyToScancodeMapping{}; ///< Mapping from Key to Scancode EnumArray<Keyboard::Key, Keyboard::Scancode, Keyboard::KeyCount> m_keyToScancodeMapping{}; ///< Mapping from Key to Scancode
EnumArray<Keyboard::Scancode, Keyboard::Key, Keyboard::ScancodeCount> m_scancodeToKeyMapping{}; ///< Mapping from Scancode to Key EnumArray<Keyboard::Scancode, Keyboard::Key, Keyboard::ScancodeCount> m_scancodeToKeyMapping{}; ///< Mapping from Scancode to Key

View File

@ -562,6 +562,13 @@ bool HIDInputManager::isKeyPressed(Keyboard::Key key)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
bool HIDInputManager::isKeyPressed(Keyboard::Scancode code) bool HIDInputManager::isKeyPressed(Keyboard::Scancode code)
{ {
// Lazy load m_keys to prevent unnecessary macOS input monitoring permission requests
if (!m_keysInitialized)
{
initializeKeyboard();
m_keysInitialized = true;
}
return (code != Keyboard::Scan::Unknown) && isPressed(m_keys[code]); return (code != Keyboard::Scan::Unknown) && isPressed(m_keys[code]);
} }
@ -716,7 +723,6 @@ HIDInputManager::HIDInputManager()
} }
// Build up our knowledge of the hardware // Build up our knowledge of the hardware
initializeKeyboard();
buildMappings(); buildMappings();
// Register for notification on keyboard layout changes // Register for notification on keyboard layout changes
@ -924,13 +930,17 @@ void HIDInputManager::freeUp()
m_manager = nil; m_manager = nil;
for (auto& key : m_keys) if (m_keysInitialized)
{ {
for (IOHIDElementRef iohidElementRef : key) for (auto& key : m_keys)
CFRelease(iohidElementRef); {
for (IOHIDElementRef iohidElementRef : key)
CFRelease(iohidElementRef);
key.clear(); key.clear();
}
} }
m_keysInitialized = false;
} }