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.

1
  • Just a question. Why do you need to constantly switch? I'm just interested. Is the computer is being used bu multiple people or something?
    – Josiah
    Nov 16, 2012 at 23:50

4 Answers 4

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
  • Thanks man! Exactly what I asked. I was bummed out by the fact that I had to logout/login so I did a bit of research. I'll add my finding.
    – iDev247
    Nov 17, 2012 at 5:43
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
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.

1
  • He said "via terminal". Oct 4, 2015 at 14:35
0

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("NXOpenEventStatus failed!\n");
            return 0;
    } 
    

    int64_t         currentButtonStatus     = 0;

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

}
2
  • Note: Depending on OS version may need to change int64_t to int32_t.
    – 1110101001
    May 20, 2023 at 20:49
  • Perfect! Thank you! I was able to compile this using clang -framework IOKit -framework Foundation mouseswap.c
    – Taeber
    Feb 13 at 1:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .