Switching Mouse Keys Off/On with code

I’m trying to switch Mouse Keys Off/On with a script (or even a keyboard shortcut if it exists). I know how to toggle this function Off/On, but at the end of the toggle I don’t know if it’s Off or On.

Ideally, I’d like 2 scripts. One to turn Mouse Keys On, and one to turn them Off. If that can’t be done, then is there a way to determine the current Off/On state of Mouse Keys, so if it’s not what I want then I can run the toggle command again.

This isn’t the most elegant thing in the world. It relies on a delay, and the System Preferences pane flashes on the screen for a sec.


tell application "System Preferences"
	reveal anchor "Mouse" of pane id "com.apple.preference.universalaccess"
	activate
	delay 0.1
	tell application "System Events"
		tell process "System Preferences"
			set mouseKeysOn to the value of checkbox "Enable Mouse Keys" of window "Accessibility"
		end tell
	end tell
	quit
end tell

But in the end, “mouseKeysOn” is 0 if it’s off and 1 if it’s on.

Works on 10.10 here. You can use it to determine the state and then do whatever you want to do. Might want to insert the “whatever you want to do” before this closes the window again.

Thanks, t.spoon. I’m afraid that window flash won’t work for my purposes, but other than that I’m surprised that there isn’t some way to get the current Off/On state back from the system without gui scripting. Using Apple’s 5-tap method of toggling Mouse Keys is quite ok at the keyboard, but definitely not ok when trying to script it with no way of finding the current state.

I could use your method except for the window flash. Is there any way to suppress that?

You could use the shell command ‘defaults’ for this, but the problem is that only the preference is set but the service is not activated, this is what the pref pane does.

On Mojave the things are little different. The following code is working and more stable for getting the status of Mouse Keys:


tell application "System Preferences"
	activate
	reveal anchor "Mouse" of pane id "com.apple.preference.universalaccess"
	tell application "System Events"
		tell application process "System Preferences"
			repeat until (exists window "Accessibility")
				delay 0.1
			end repeat
			set mouseKeysOn to the value of checkbox "Enable Mouse Keys" of group 1 of window "Accessibility"
		end tell
	end tell
	quit
end tell
mouseKeysOn

To enable/disable the switching On/Off Mouse Keys using 5 OPTION KEY taps (this second is possible only manually, and that is OK for the security), use the following code:


tell application "System Preferences"
	activate
	reveal anchor "Mouse" of pane id "com.apple.preference.universalaccess"
	tell application "System Events"
		tell application process "System Preferences"
			repeat until (exists window "Accessibility")
				delay 0.1
			end repeat
			click checkbox "Enable Mouse Keys" of group 1 of window "Accessibility"
		end tell
	end tell
	quit
end tell