diff --git a/src/SFML/Window/macOS/HIDInputManager.hpp b/src/SFML/Window/macOS/HIDInputManager.hpp index 52397b11f..2b078fd0c 100644 --- a/src/SFML/Window/macOS/HIDInputManager.hpp +++ b/src/SFML/Window/macOS/HIDInputManager.hpp @@ -286,7 +286,8 @@ private: //////////////////////////////////////////////////////////// // 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 m_keys; ///< All the keys on any connected keyboard EnumArray m_keyToScancodeMapping{}; ///< Mapping from Key to Scancode EnumArray m_scancodeToKeyMapping{}; ///< Mapping from Scancode to Key diff --git a/src/SFML/Window/macOS/HIDInputManager.mm b/src/SFML/Window/macOS/HIDInputManager.mm index 05bd9ec57..829901f55 100644 --- a/src/SFML/Window/macOS/HIDInputManager.mm +++ b/src/SFML/Window/macOS/HIDInputManager.mm @@ -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,13 +930,17 @@ void HIDInputManager::freeUp() m_manager = nil; - for (auto& key : m_keys) + if (m_keysInitialized) { - for (IOHIDElementRef iohidElementRef : key) - CFRelease(iohidElementRef); + for (auto& key : m_keys) + { + for (IOHIDElementRef iohidElementRef : key) + CFRelease(iohidElementRef); - key.clear(); + key.clear(); + } } + m_keysInitialized = false; }