diff --git a/src/blackinput/macos/macosinpututils.h b/src/blackinput/macos/macosinpututils.h index be519a23f8..d863983bb9 100644 --- a/src/blackinput/macos/macosinpututils.h +++ b/src/blackinput/macos/macosinpututils.h @@ -16,6 +16,9 @@ namespace BlackInput public: CMacOSInputUtils() = delete; + //! Check OS permission for input monitoring access + static bool hasAccess(); + //! Request OS permission for input monitoring access static bool requestAccess(); diff --git a/src/blackinput/macos/macosinpututils.mm b/src/blackinput/macos/macosinpututils.mm index 0237f3142e..7fdf3a99d6 100644 --- a/src/blackinput/macos/macosinpututils.mm +++ b/src/blackinput/macos/macosinpututils.mm @@ -8,6 +8,19 @@ namespace BlackInput { + + bool CMacOSInputUtils::hasAccess() + { + if (@available(macOS 10.15, *)) + { + return IOHIDCheckAccess(kIOHIDRequestTypeListenEvent) == IOHIDAccessType::kIOHIDAccessTypeGranted; + } + else + { + return true; + } + } + bool CMacOSInputUtils::requestAccess() { if (@available(macOS 10.15, *)) diff --git a/src/swiftguistandard/CMakeLists.txt b/src/swiftguistandard/CMakeLists.txt index 1c746d7ddc..8c4aeb8ebd 100644 --- a/src/swiftguistandard/CMakeLists.txt +++ b/src/swiftguistandard/CMakeLists.txt @@ -29,6 +29,8 @@ target_link_libraries(swiftguistd ) if(APPLE) + target_link_libraries(swiftguistd PUBLIC input) + set_target_properties(swiftguistd PROPERTIES MACOSX_BUNDLE TRUE) set_target_properties(swiftguistd PROPERTIES MACOSX_BUNDLE_INFO_PLIST ${CMAKE_CURRENT_SOURCE_DIR}/Info.plist) set(RESOURCE_FILES swift.icns qt.conf) diff --git a/src/swiftguistandard/swiftguistd.cpp b/src/swiftguistandard/swiftguistd.cpp index 4bf63fa99b..bde53a53dc 100644 --- a/src/swiftguistandard/swiftguistd.cpp +++ b/src/swiftguistandard/swiftguistd.cpp @@ -28,6 +28,10 @@ #include "blackmisc/threadutils.h" #include "blackconfig/buildconfig.h" +#if defined(Q_OS_MACOS) +# include "blackinput/macos/macosinpututils.h" +#endif + #include "swiftguistd.h" #include #include @@ -475,6 +479,16 @@ void SwiftGuiStd::verifyPrerequisites() msgs.push_back(sGui->getIContextSimulator()->verifyPrerequisites()); } +#if defined(Q_OS_MACOS) + if (!BlackInput::CMacOSInputUtils::hasAccess()) + { + // A log message about missing permissions is already emitted when initializing the keyboard. + // But this happens way before initializing the GUI. Hence do the check here again to show an error message + // to the user + msgs.push_back(CLogMessage(this).error(u"Cannot access the keyboard. Is \"Input Monitoring\" for swift enabled?")); + } +#endif + if (msgs.hasWarningOrErrorMessages()) { if (msgs.size() > 1) { this->displayInOverlayWindow(msgs); }