macOS / Cocoa / SwiftUI - How can I tell if the user prefers left-click as secondary-click?

310 Views Asked by At

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.

1

There are 1 best solutions below

1
TheNextman On BEST ANSWER

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"));