[UIKit/GameController] Is there any way to detect middle mouse button up and down in iOS without using GCMouse (maybe use UIKit)?

I found that GCMouse not work when I run iOS app on M1 Mac directly, so I want to detect and handle the mouse event via gestureRecognizer or other way. But is there any way to handle middle/right button easily?

Replies

Yes, UIEvent has a UIEventButtonMask since iOS / iPad OS 13.4:


/// Set of buttons pressed for the current event
/// Raw format of: 1 << (buttonNumber - 1)
/// UIEventButtonMaskPrimary = 1 << 0
typedef NS_OPTIONS(NSInteger, UIEventButtonMask) {
    UIEventButtonMaskPrimary    = 1 << 0,
    UIEventButtonMaskSecondary  = 1 << 1
} NS_SWIFT_NAME(UIEvent.ButtonMask) API_AVAILABLE(ios(13.4)) SPI_AVAILABLE(tvos(13.4), watchos(6.2));


/// Convenience initializer for a button mask where `buttonNumber` is a one-based index of the button on the input device
/// .button(1) == .primary
/// .button(2) == .secondary
UIKIT_EXTERN UIEventButtonMask UIEventButtonMaskForButtonNumber(NSInteger buttonNumber) NS_SWIFT_NAME(UIEventButtonMask.button(_:)) API_AVAILABLE(ios(13.4)) API_UNAVAILABLE(tvos, watchos);

@property (nonatomic, readonly) UIEventButtonMask buttonMask API_AVAILABLE(ios(13.4)) API_UNAVAILABLE(tvos, watchos);

And UIGestureRecognizer has a similar convenience:

// Values from the last event processed.
// These values are not considered as requirements for the gesture.
@property (nonatomic, readonly) UIKeyModifierFlags modifierFlags API_AVAILABLE(ios(13.4)) SPI_AVAILABLE(tvos(13.0), watchos(6.0));
@property (nonatomic, readonly) UIEventButtonMask buttonMask API_AVAILABLE(ios(13.4)) API_UNAVAILABLE(tvos, watchos);

Some mice are different, so you may need to figure out which button number the middle or right buttons are, but you can use UIEventButtonMaskForButtonNumber() to help figure that out.

The response here seems to imply it is possible to pick up the middle mouse button with UIEventButtonMaskForButtonNumber(), but after trying 3,5,4,6, etc. I am never seeing my gestureRecognizer being called. Specifically I am using:

oneFingerTapRecognizer.buttonMaskRequired = UIEvent.ButtonMask.button(3)

Can someone clarify if detecting the middle mouse button (on a bluetooth) is really possible?