laike9m
laike9m

Reputation: 19368

How do I capture the two-finger tap event on status bar?

I'm building a mac app with status bar icon. I want to enable two-finger tap guesture when tapping on the icon (note: physically, you tap the touchpad). Tried the following code, but even if I use button.sendAction(on: .any), handleTwoFingerTap is not triggered.

class StatusBar {
  var statusBar: NSStatusBar!
  var statusBarItem: NSStatusItem!

  init() {
    statusBar = NSStatusBar()
    statusBarItem = statusBar.statusItem(
      withLength: NSStatusItem.variableLength
    )
    if let button = statusBarItem.button {
      // code to set up icon is ignored

      // Doesn't work
      button.sendAction(on: .any)
      button.action = #selector(handleTwoFingerTap(_:))
      button.target = self
    }
  }

    @objc private func handleTwoFingerTap(_: Any?) {
      // this function is not called
    }
}

I suspect this is due to the limitation of sendAction, quote

The only conditions that are actually checked are associated with the NSLeftMouseDownMask, NSLeftMouseUpMask, NSLeftMouseDraggedMask, and NSPeriodicMask bits.

In that case, how to make this work?

Upvotes: 0

Views: 48

Answers (0)

Related Questions