1

macOS defaults to assuming the user's mouse is in their right hand, so their secondary click is a "right-click". Some users hold their mouse in their left hand, so their secondary click is a "left-click".

I want to write some text in a dialog describing whether to perform a primary-click or a secondary-click. However, I think that the term "secondary-click" is not common enough to be understood by most users. How can I check the system preference for the handedness of the mouse, so I can swap out the dialog's text depending on the user's preference?

The frameworks I'm using are SwiftUI and Cocoa, but I'd be open to using another if that's the only way to solve this.

Ky -
  • 30,724
  • 51
  • 192
  • 308
  • Don't forget that secondary-click can be disabled altogether. Also, some mice only have a single button (and aren't able to distinguish through multi-touch). For example, ancient hockey-puck mice that came with the original iMac. – Ken Thomases Feb 14 '20 at 00:32
  • @KenThomases Good point! In that case, how can I detect if the user is using an input device which does not support secondary-click, or which has it disabled? – Ky - Feb 14 '20 at 19:29
  • 1
    Not sure. I didn't see any preference settings change when I toggled it. It's probably not some userland needs to see. It probably affects the driver's behavior. – Ken Thomases Feb 14 '20 at 21:11
  • @KenThomases That's fair. I guess until that's possible to detect (or, more likely, until SwiftUI registers secondary-clicks), I'll still throw up this instruction. – Ky - Feb 15 '20 at 05:23

1 Answers1

2

You can read the user default com.apple.mouse.swapLeftRightButton

From Terminal, run:

defaults -currentHost read -globalDomain com.apple.mouse.swapLeftRightButton

If the key does not exist, the button is not swapped (i.e. the primary button is "Left").

From Cocoa, this can be read using NSUserDefaults:

Swift

UserDefaults.standard.bool(forKey: "com.apple.mouse.swapLeftRightButton")

Objective-C

NSString* const SwapLeftRightButtonKey = @"com.apple.mouse.swapLeftRightButton";
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSNumber *swap = [userDefaults objectForKey:SwapLeftRightButtonKey];
NSLog(@"com.apple.mouse.swapLeftRightButton == %@",
            ([swap boolValue] ? @"YES" : @"NO"));
Ky -
  • 30,724
  • 51
  • 192
  • 308
TheNextman
  • 12,428
  • 2
  • 36
  • 75
  • Works for me! Now to find out how to make SwiftUI react to this preference changing... – Ky - Feb 13 '20 at 23:04