Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the '10.5: Set different modifier keys for external keyboards' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.5: Set different modifier keys for external keyboards
Authored by: Anonymous on Oct 26, '07 08:05:23PM
For people not upgrading...
I have an external keyboard, that I swap the command/alt keys (so the laptop and external keyboard command buttons are next to the spacebar)

I was doing this manually, which got tedious quickly. I found a script that automatically swapped they keys. Since I found it slightly confusing (ended up swapping it away from the right settings and such), among other problems (it reset my spacebar-as-shift option) - I rewrote it a bit.


tell application "System Preferences"
	activate
	set current pane to pane "com.apple.preference.keyboard"
end tell


tell application "System Events"
	-- If we don't have UI Elements enabled, then nothing is really going to work.
	if UI elements enabled then
		tell application process "System Preferences"
			get properties
			
			-- Open up the Modifier Keys sheet
			click button "Modifier Keys…" of tab group 1 of window "Keyboard & Mouse"
			tell sheet 1 of window "Keyboard & Mouse"
				-- get the text of the 3rd pop up button
				set commandKey to value of pop up button 3
				-- looks like we're in laptop mode.  Swap the keys
				click pop up button 3
				click menu item 4 of menu 1 of pop up button 3 -- command key to alt
				delay 1
				click pop up button 4
				click menu item 3 of menu 1 of pop up button 4 -- alt key to command
				-- close the sheet
				click button "OK"
			end tell
		end tell
		tell application "System Preferences" to quit
	else
		-- UI elements not enabled.  Display an alert
		tell application "System Preferences"
			activate
			set current pane to pane "com.apple.preference.universalaccess"
			display dialog "UI element scripting is not enabled.
              Check \"Enable access for assistive devices\""
		end tell
	end if
end tell
I saved it as lt_to_pc.scpt, and the same script. And another version with the "of popup button 3" changed to button 4, and "button 4" to button 3 - saved as pc_to_lt.
I run it automatically via MarcoPolo (two zones, PC_keyboard, and LT_keyboard - when it arrives in PC_keyboard, it runs lt_to_pc, and for LT_keyboard, pc_to_lt, The zones are switched by the USB keyboard device of course) - but I'm considering just running it via Quicksilver, unless I can think of other uses fo MarcoPolo (Currently I don't have any other need for it, so running it just to swap the keyboard keys is slightly overkill I'd say)..

Apologies for the slightly incoherent post.. - Ben

[ Reply to This | # ]
10.5: Set different modifier keys for external keyboards
Authored by: Anonymous on Dec 19, '07 11:24:01PM
It seems some external keyboards don't get recognized by Leopard, so the automatic modifier switching doesn't work for them. Yury Soldak emailed me yesterday, with a modified version of the above code which works with the Leopard (the drop-down indexes changed because of the extra button, and there's a few other changes) Here's the code - to swap the command and option keys on an external keyboard on Leopard: (Its an AppleScript, copy and paste it into Script Editor and save/run it)
tell application "System Preferences"
	activate
	set current pane to pane
	"com.apple.preference.keyboard"
end tell


tell application "System Events"
	-- If we don't have UI Elements enabled, then nothing is really going to work.
	if UI elements enabled then
		tell application process "System Preferences"
			get properties
			
			-- Open up the Modifier Keys sheet
			click button "Modifier Keys…" of tab group 1 of window "Keyboard & Mouse"
			tell sheet 1 of window "Keyboard & Mouse"
				if pop up button 5 exists then
					-- looks like we have external keyboard.  Swap the keys for all
					click pop up button 5
					click menu item "All" of menu 1 of pop up button 5
					delay 1
					-- command key to alt
					click pop up button 1
					click menu item 3 of menu 1 of pop up button 1
					delay 1
					-- alt key to command
					click pop up button 2
					click menu item 4 of menu 1 of pop up button 2
					delay 1
					-- restore defaults for internal keyboard
					click pop up button 5
					click menu item "Apple Internal Keyboard / Trackpad" of menu 1 of pop up button 5
					delay 1
					click button "Restore Defaults"
				else
					click button "Restore Defaults"
				end if
				-- close the sheet
				click button "OK"
			end tell
		end tell
		tell application "System Preferences" to quit
	else
		-- UI elements not enabled.  Display an alert
		tell application "System Preferences"
			activate
			set current pane to pane "com.apple.preference.universalaccess"
			display dialog "UI element scripting is not enabled.
             Check \"Enable access for assistive devices\""
		end tell
	end if
end tell


[ Reply to This | # ]