【问题标题】:Is there a way to receive macOS events while users hold down a mouse button?有没有办法在用户按住鼠标按钮时接收 macOS 事件?
【发布时间】:2020-10-16 20:33:53
【问题描述】:

拥有 SwiftUI slider。 用户继续拖动它。 拖动时,用户还按下了Option 键。

我想根据键盘修饰符标志的更改(例如 Option 键)更改用户界面。但是,在拖动滑块甚至按下鼠标按钮时,似乎主事件循环被阻塞了。

当使用NSEvent.addLocalMonitorForEvents(mathing:handler) 获得Option 按键的通知时,处理程序甚至不会在用户拖动滑块时被调用。

有什么方法可以实现吗?

我也很想了解为什么首先存在问题。

【问题讨论】:

    标签: swift macos swiftui


    【解决方案1】:

    最后,我最终使用了一个计时器块来检查和发布键盘修饰符标志的更改。

    import Cocoa
    import Combine
    
    /**
        Publishers for keyboard events.
    
        The modifiers are checked regularly by a Timer running on the Main `RunLoop` in `.common` mode
        to ensure the modifiers are detected even when having mouse button pressed down
        (which normally blocks the run loops on the main thread which are not in `common` mode).
    
        The `NSEvent.addLocalMonitorForEvents` cannot be used because the handler is not executed
        for nested-tracking loops, such as control tracking (e.g. when a mouse button is down).
     */
    public class KeyboardEvents {
        /// Broadcasts keyboard flag changes (e.g. keys like Option, Command, Control, Shift, etc.)
        public let modifierFlagsPublisher: AnyPublisher<NSEvent.ModifierFlags?, Never>
    
        private let modifierFlagsSubject = PassthroughSubject<NSEvent.ModifierFlags?, Never>()
        private var timer: Timer!
    
        public init() {
            modifierFlagsPublisher = modifierFlagsSubject.removeDuplicates().eraseToAnyPublisher()
    
            timer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { [unowned self] _ in
                modifierFlagsSubject.send(NSApp.currentEvent?.modifierFlags)
            }
    
            RunLoop.current.add(timer, forMode: .common)
        }
    
        deinit {
            timer.invalidate()
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多