3

I'm looking for a way to swap the left/right button on my mouse (left/right handed person) via the Terminal.

Currently every time I want to switch this I need to go to System Preferences » Mouse » Change the option for Secondary click. It would be much easier to just run a quick command in the terminal.

I've seen things like this: defaults write GlobalPreferences com.apple.mouse.scaling -1

tl;dr: Is there a way to do this sort of command (above) for the mouse's secondary click via the terminal?

My OS is 10.7.5.

iDev247
  • 317

4 Answers4

5

The relevant command is:

defaults -currentHost write .GlobalPreferences com.apple.mouse.swapLeftRightButton -bool true

(or "false" to set it back to normal.) However, I don't know of a way to get the setting to take effect without logging out & back in, which is generally more trouble than using System Preferences to make the change.

1

Can do via IOHID directly:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <IOKit/hidsystem/IOHIDLib.h>
#include <IOKit/hidsystem/IOHIDParameter.h>
#include <IOKit/hidsystem/event_status_driver.h>

int main(int argc, char *argv[]) { io_connect_t handle = NXOpenEventStatus();

if (!handle) {
        printf(&quot;NXOpenEventStatus failed!\n&quot;);
        return 0;
} 


int64_t         currentButtonStatus     = 0;


kern_return_t ret;

IOByteCount actualSize;
ret = IOHIDGetParameter(handle, CFSTR(kIOHIDPointerButtonMode),
    sizeof(currentButtonStatus), &amp;currentButtonStatus, &amp;actualSize);

if (ret != KERN_SUCCESS) {
        printf(&quot;IOHIDGetParameter failed! Error %d.\n&quot;,  (int)ret);
}
else if (actualSize != sizeof(currentButtonStatus)) {
        printf(&quot;IOHIDGetParameter returned unexpected actualSize! (Got %d)\n&quot;, (int)actualSize);
}
else if (ret == KERN_SUCCESS) {
    int64_t newButtonStatus = 3 - currentButtonStatus;
    ret = IOHIDSetParameter(handle, CFSTR(kIOHIDPointerButtonMode), &amp;newButtonStatus, sizeof(newButtonStatus));
    if (ret != KERN_SUCCESS) {
            printf(&quot;IOHIDSetParameter failed! Error %d. (Current status = %d)\n&quot;, (int)ret, currentButtonStatus);
    }
    else {
            printf(&quot;success. Was %d, now %d.\n&quot;, currentButtonStatus, newButtonStatus);
    }
}

NXCloseEventStatus(handle);
return 0;

}

1

I finally went with this solution.

Another tip that I found in this question is to add the script to the menu bar by doing this:

  1. Open AppleScripts Editor (I just search on Spotlight)
  2. If you don't already have the scripts icon in the menu bar:
    1. Click on AppleScripts Editor in the menu bar
    2. Click Preferences
    3. Enable the checkbox Show Script menu in menu bar. I personally prefer unchecking Show Computer script.
  3. Click on the scripts icon that is now showing on your menu bar
  4. Click on Open Scripts Folder and select Open User Script Folder
  5. Save or copy your script file in this folder (the script I'm using can be found here)
  6. When you want to run the script just click the script icon and select the script
iDev247
  • 317
1

Here is a simpler script that toggles the setting and doesn't activate System Preferences:

tell application "System Preferences"
    reveal pane "com.apple.preference.mouse"
end tell
tell application "System Events" to tell process "System Preferences"
    tell radio group 1 of window 1
        if value of radio button 1 is 1 then
            click radio button 2
        else
            click radio button 1
        end if
    end tell
end tell
quit application "System Preferences"

It doesn't work with the Magic Mouse version of the preference pane.

You can save the script in AppleScript Editor and run it with osascript script.scpt. Or see this question for different ways to assign shortcuts to scripts.

Lri
  • 42,502
  • 8
  • 126
  • 159