Download and install keyboardViewer:
sudo install keyboardViewer /usr/local/bin
Trigger with an AppleScript or shell script, e.g.:
tell application "System Events" if exists (process "Keyboard Viewer") then click process "Keyboard Viewer"'s window 1's buttons whose subrole is "AXCloseButton" else do shell script "/usr/local/bin/keyboardViewer" end if end tell
The above will show Keyboard Viewer if it’s not running, and close it if it is.
I run the above script from FastScripts.
The keyboardViewer utility isn’t necessary on Mac OS X 10.5 because simply opening KeyboardViewerServer does the same thing.