Is there a way to turn on and off the Mouse Keys feature using AppleScript or a terminal command?

Here's the GUI scripting solution:

tell application "System Preferences"
    reveal anchor "Mouse" of pane id "com.apple.preference.universalaccess"
    tell application "System Events"
        tell process "System Preferences"
            tell first window
                tell first tab group
                    tell radio group 1
                        if value of radio button 1 is 1 then
                            # enabled, so disable
                            click radio button 2
                        else
                            # disabled, so enable
                            click radio button 1
                        end if
                    end tell
                end tell
            end tell
        end tell
    end tell
    quit
end tell

It will launch System Preferences if required, but doesn't show the window and quits it afterwards, so from a UI POV, it at least doesn't show the window.


You can change the persistent setting on which the System Preferences configuration is based using the following commands:

defaults write ~/Library/Preferences/com.apple.universalaccess mouseDriver -bool YES
defaults write ~/Library/Preferences/com.apple.universalaccess mouseDriver -bool NO

Unfortunately, this only changes the configuration file and does not apply the changes to the running system.


While you can programmatically press Option keys using AppleScript, like the following:

tell application "System Events"
    key down option
    key up option
end tell

The press Option five times to toggle setting is at a higher level and doesn't get triggered by this. While all regular Option key presses are ignored after you only run key down option, you can still toggle Mouse Keys.


Debugging System Preferences while changing the setting shows calls to LaunchUAServerIfNeeded and UAMouseKeysSetEnabled in the private UniversalAccessCore framework. Unless we reverse engineer that framework and write our own binaries, I don't think we'll get a proper programmatic way to do this.