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

@ -287,6 +287,7 @@ private:
// Member data
////////////////////////////////////////////////////////////
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::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

View File

@ -562,6 +562,13 @@ bool HIDInputManager::isKeyPressed(Keyboard::Key key)
////////////////////////////////////////////////////////////
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]);
}
@ -716,7 +723,6 @@ HIDInputManager::HIDInputManager()
}
// Build up our knowledge of the hardware
initializeKeyboard();
buildMappings();
// Register for notification on keyboard layout changes
@ -924,6 +930,8 @@ void HIDInputManager::freeUp()
m_manager = nil;
if (m_keysInitialized)
{
for (auto& key : m_keys)
{
for (IOHIDElementRef iohidElementRef : key)
@ -932,6 +940,8 @@ void HIDInputManager::freeUp()
key.clear();
}
}
m_keysInitialized = false;
}
////////////////////////////////////////////////////////////