Apple - How to show or hide Keyboard Viewer with a keyboard shortcut?

Launch Keyboard Viewer with a Service (Improved)

You can launch the Keyboard Viewer with a shortcut by using Automator and the OS X Services functionality.

The Keyboard Viewer program lives at /System/Library/Input Methods/KeyboardViewer.app (in versions prior to Lion, it may be at /System/Library/Components/KeyboardViewer.component/Contents/SharedSupport/KeyboardViewerServer.app). You open it with a hotkey by using Automator to create a simple launcher service.

  1. Open Automator and select Service as the type of your new document.
  2. Set the options (at the top of the workflow area) to "Service receives no input in any application".
  3. Add the Run AppleScript action to your workflow, and replace the text with the following lines:

    if application "KeyboardViewer" is running then
        quit application "KeyboardViewer"
    end if
    
    activate application "KeyboardViewer"
    
    -- wait until the window has been closed, then end the KeyboardViewer process
    set numberOfWindows to 1
    repeat until numberOfWindows = 0
        delay 5
        tell application "System Events"
            tell process "KeyboardViewer"
                set numberOfWindows to count windows
            end tell
        end tell
    end repeat
    quit application "KeyboardViewer"
    
  4. Save with a name like "Open Keyboard Viewer", then open Keyboard Preferences to the Keyboard Shortcuts tab. Select Services in the left pane and scroll to the bottom, where you should see the name of your Service under the General Section.
  5. Make sure the box is checked to enable it, then select it and click add shortcut to set a hotkey.
  6. After setting the hotkey, open the Services menu in any application (i.e. Finder > Services), then close it. For some reason my hot key didn't work until I did this.

A couple notes:

  • The script requires that you check the Enable access for assistive devices box in the Universal Access preference pane.
  • Closing the Keyboard Viewer window doesn't actually quit the application, and as Lri points out, it can be a bit of a resource hog, so the repeat loop checks every 5 seconds if Keyboard Viewer has any open windows, and if not, quits the process.
  • If you're running a pre-Lion OS, you may need to replace the instances of KeyboardViewer with KeyboardViewerServer. I don't have anything pre-Lion handy to test this (if someone else could report back in the comments, that would be great.
  • Because the script loops until the Keyboard Viewer is closed, the Automator spinning gear icon will show in the menu bar until it closes.

Glad I found this thread. Based on the answers above, I made an Alfred extension script that toggles Keyboard Viewer.

https://dl.dropbox.com/u/29440342/linkedFiles/Keyboard%20Viewer.alfredextension

It provides an alfred keyword "kv" that toggles Keyboard Viewer on and off, by running the following applescript:

-- adapted from http://apple.stackexchange.com/a/62532
if application "KeyboardViewer" is running then
    quit application "KeyboardViewer"
else
    activate application "KeyboardViewer"
end if

(*
-- adapted from http://apple.stackexchange.com/a/59986
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
*)
-- icon ripped from osx /System/Library/PreferencePanes Keyboard.prefPane/Contents/Resources/Keyboard.icns

I left an alternative implementation commented out, in case I experience performance problems that it was meant to avoid.


The Extra Scripts Plugin of Quicksilver offers this functionality by providing Show Keyboard Viewer.scpt.

Also, when you create a Quicksilver Trigger for Show Keyboard Viewer.scpt with the action Run, then you get the global Keyboard Viewer hotkey.

Here's the source of the script:

property theApplication : "KeyboardViewer"
property thePath : "/System/Library/Input Methods/KeyboardViewer.app"

set HFSPath to ((POSIX file thePath) as string)
tell application "System Events" to ¬
    set isRunning to 0 < (count (application processes whose name is theApplication))
if isRunning then
    tell application HFSPath to quit
else
    ignoring application responses
        tell application HFSPath to activate
    end ignoring
end if

If KeyboardViewer is opened with an AppleScript or with open and the main window is closed, the Keyboard Viewer process stays open and keeps using about 0-20% CPU.