Sunday, September 28, 2008

Swap Modifier Keys Between Macbook and External Windows Keyboard Modes

A problem with PC keyboards on the Mac is that your Option and Command keys get mapped to the Windows and Alt keys, respectively (this swaps their placement). This makes it confusing when you are switching between a Macbook's onboard keyboard and USB PC keyboard.

Well, in Keyboard & Mouse preferences you can swap the modifier keys (Option, Command, Control, and Caps Lock) to remedy this. In Tiger, you could only change this globally for all keyboards, meaning that when you switched between external and internal keyboards, you'd have to keep switching the settings if you wanted to keep the right key orientation. This gave rise to the Applescript below, from Lance Ball.

Keyboard Prefs Screenshot
In Leopard, however, they added the ability to configure keyboards independently. You could, theoretically, leave your internal keyboard with the default setting, and only have them swapped for the external. Oh, if only this were the case. I run a Logitech Cordless Desktop MX3000, and used to, for a brief moment, have the option to do this. It was listed as "USB Reciever". However, the Logitech Control Center driver removed this. That's what brought me to the following script. This will automatically swap the Option and Command key mappings, toggling between them each time it is run. The original Lance Ball script was for Tiger, but has been adapted for Leopard. You can use this with MarcoPolo to run automatically when you add/remove your keyboard.

Update (9/29/08): I gave up using my MX3000 mouse, and went to a more ergonomic MS Natural Mouse. Since the MX3000 keyboard never worked with Logitech Control Center anyway, I just removed the driver. I've now reclaimed the ability to independently map my Logitech keyboard. Huzzah.

If you still need the Leopard script for some reason, here it is. I added code to swap the MBP’s keyboard illumination on/off (off for ‘docked’ mode. You can enable this feature as per the script comments since it's disabled by default.

Paste and save the following in Script Editor.
-- **********************************************************************************
-- Utility script to switch keyboard mapping for Command and Option keys.
-- Useful when you have a PC external keyboard that you use in one location, but
-- at other times you are using the builtin laptop keyboard or an Apple keyboard.
-- Author: Lance Ball (lanceball - at - mac - dot - com)
-- Modified by: Joe Chan
-- Modified attributes: Script will change Internal Keyboard to have default behavior when
-- changing keyboards
--
-- Leopard 10.5.5 version by netgarou. Also added feature to toggle Macbook Pro keyboard illumination.
-- (Remove the appropriate remark lines that start with "--**" to enable this if your system supports it).
-- Also disabled Growl notifications by default.
--
-- **********************************************************************************

-- Set up growl notifications
tell application "GrowlHelperApp"
-- Make a list of all the notification types
-- that this script will ever send:
set the allNotificationsList to ¬
{"External Keyboard Mode Notification", "Default Keyboard Mode Notification"}

-- Make a list of the notifications
-- that will be enabled by default.
-- Those not enabled by default can be enabled later
-- in the 'Applications' tab of the growl prefpane.
set the enabledNotificationsList to ¬
{"External Keyboard Mode Notification", "Default Keyboard Mode Notification"}

-- Register our script with growl.
-- You can optionally (as here) set a default icon
-- for this script's notifications.
register as application ¬
"Change Keyboard AppleScript" all notifications allNotificationsList ¬
default notifications enabledNotificationsList ¬
icon of application "Script Editor"
end tell

-- Open System Preferences
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
--If keyboard illumination is on, disable it when the external keyboard is connected.
--**if value of checkbox "Illuminate keyboard in low light conditions" of group 1 of tab group 1 of window "Keyboard & Mouse" is 1 then
--** click checkbox "Illuminate keyboard in low light conditions" of group 1 of tab group 1 of window "Keyboard & Mouse"
--**end if
-- 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 areweDefault to "0"
set commandKey to value of pop up button 2
-- If we're in default mode, swap the keys
if commandKey ends with "Option" then
-- Change "All" keyboards
click pop up button 2
click menu item 4 of menu 1 of pop up button 2
delay 0.1
click pop up button 1
click menu item 3 of menu 1 of pop up button 1
delay 0.1
-- Send growl notification
--** tell application "GrowlHelperApp"
--notify with name ¬
--** "External Keyboard Mode Notification" title ¬
--** "External Keyboard Mode" description ¬
--** "Settings have been optimized for external keyboards" application name "Change Keyboard AppleScript"
--** end tell
--** delay 0.1
click button "OK"
else
-- We're in PC keyboard mode. Swap back to the defaults
click button "Restore Defaults"
--** set areweDefault to "1"
-- Send growl notification
--** tell application "GrowlHelperApp"
--notify with name ¬
--** "Default Keyboard Mode Notification" title ¬
--** "Default Keyboard Mode" description ¬
--** "Reverted to default keyboard settings" application name "Change Keyboard AppleScript"
--**end tell
click button "OK"
end if
-- close the sheet
end tell
--** If switching back into laptop keyboard mode, then reenable the keyboard illumination if it was disabled
--** if areweDefault is "1" then
--** if value of checkbox "Illuminate keyboard in low light conditions" of group 1 of tab group 1 of window "Keyboard & Mouse" is 0 then
--** click checkbox "Illuminate keyboard in low light conditions" of group 1 of tab group 1 of window "Keyboard & Mouse"
--** end if
--**end if

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

Spore: Round One

I started playing Spore this week.  I don't know how the online reviewers could claim that the performance was good even on less powerful machines.  I have it running on my Macbook Pro 2.2ghz (Geforce 8600M 128MB) with all the settings turned down, and it *CRAWLS* once past the Cell stage.  Anyway, here  are two of the products from my game: my species, and a land vehicle for them. Although not much of a game at this point in development, the creator tools really are amazing.  I'm looking forward to seeing what else I can come up with.

Yeah, it looks kinda funny... but I'm proud of the duck head.  It reminds me of the one on Wade's swimming tube from the US Acres/Garfieldd & Friends cartoon.



[caption id="" align="alignright" width="250" caption="Wade Duck"][/caption]

As you progress to each new stage (Cell->Creature->Tribal->Nation->etc) your species develops one of three specializations depending on your choices in the last stage.  For some reason mine became faith/culturally based.  Thus in the Civilization stage I must use faith to convert other rival nations.  Hence, this guy here is the "Faithinator 1000".  Those loudspeakers coming out the side blare the word of Cthulhu to the heathen.  I'm somewhat proud of the VW-Hippopotamus look that it's taken on, and only wish the legs would articulate when the unit is moving in the game (what's the point in putting in legs if you can't see them walk?).