4

Just playing with the new mouse/trackpad support in iOS13.4 and trying to detect both primary and secondary mouse button clicks.

I can detect them (primary OR secondary clicks) by setting the buttonMaskRequired on a UITapGestureRecognizer to EITHER UIEventButtonMaskPrimary or UIEventButtonMaskSecondary

BUT if I attempt to recognise both (buttonMaskRequired = UIEventButtonMaskPrimary | UIEventButtonMaskSecondary) then the gesture recogniser doesn't fire (for mouse clicks.)

Anyone know if this is an 'oversight' or I'm doing something wrong?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Scotty
  • 2,019
  • 1
  • 20
  • 31
  • You'd probably need two distinct recognizers. If you set the two flags in a single mask, it probably means that both buttons are required to click simultaneously. – Cœur Mar 27 '20 at 10:37
  • There's `buttonMask` property on `UIGestureRecognizer` that should help determining which mouse button click was used. https://developer.apple.com/documentation/uikit/uigesturerecognizer/3538974-buttonmask However, in my testing (iPadOS 13.5) the property is always of `rawValue 0` no matter which mouse button you use. Seems like a bug in iOS. – Tom Kraina May 26 '20 at 14:06

1 Answers1

0

I have since made progress - but it feels real hacky.

Create 2 tap gesture recognisers - one with primary mask set, and the other with secondary mask set. I then kill the secondary gesture in the gestureRecognizerShouldBegin: delegate call if the UITouch type is UITouchTypeDirect so as to stop use getting 2 tap events with a finger.

Scotty
  • 2,019
  • 1
  • 20
  • 31
  • 1
    I've overridden `gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch)` but touch's type is always `.direct`, even when using a mouse – Steve Kuo May 31 '20 at 21:47
  • @SteveKuo Does it work in your Mac Catalyst App? ...the UIEventButtonMaskSecondary seems to work only using CTRL+CLICK ... if I change the trackpad settings from Catalina's Settings the Secondary click is not detected through UIEventButtonMaskSecondary – Marco Cattai Jun 15 '20 at 18:12