Keyboard method for opening the Mac's On-screen Keyboard
See this solution for Mac OS X 10.6.
On Mac OS X 10.5, you can open the following program:
/System/Library/Components/KeyboardViewer.component/Contents/SharedSupport/KeyboardViewerServer.app/Contents/MacOS/KeyboardViewerServer
At least in 10.7 and later you can just open the KeyboardViewer application:
open -a KeyboardViewer
It has a few drawbacks though:
- If you close the keyboard window by for example pressing the close button, the KeyboardViewer process stays running and keeps using something like 0-10% CPU.
- If you open KeyboardViewer again after that, it doesn't reopen the keyboard window.
- The keyboard window is made visible when I run scripts that emulate keystrokes or use Alfred's clipboard history.
All of those can be avoided by opening the keyboard window from the input menu:
tell application "System Events" to tell process "SystemUIServer"
tell (menu bar item 1 where description is "text input") of menu bar 1
click
click (menu item 1 where title ends with "Keyboard Viewer") of menu 1
end tell
end tell
A workaround for the second issue is to terminate KeyboardViewer if it is running but has no windows:
if application "KeyboardViewer" is running then
tell application "System Events" to number of windows of process "KeyboardViewer"
if result is 0 then
quit application "KeyboardViewer"
delay 0.1
activate application "KeyboardViewer"
else
quit application "KeyboardViewer"
end if
else
activate application "KeyboardViewer"
end if