How to show or hide Keyboard Viewer with a keyboard shortcut?
I would like to display and hide the Keyboard Viewer using a shortcut.
- Is there a way to display the Keyboard Viewer via a keyboard shortcut?
- Is there a way to automatically display the Keyboard Viewer once I switch to a specific language?
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.
- Open Automator and select Service as the type of your new document.
- Set the options (at the top of the workflow area) to "Service receives no input in any application".
-
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"
- 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.
- Make sure the box is checked to enable it, then select it and click add shortcut to set a hotkey.
- 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
withKeyboardViewerServer
. 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.
I have found a way through BetterTouchTool to do the following :
- Activate Keyboard Viewer by a shortcut
- Close Keyboard Viewer by another shortcut
- Toggle Keyboard Viewer by one shortcut (no need for the previous two)
- I also uploaded the AppleScript for convenience
Platform : rMBP Mountain Lion OSX 10.8
Activate Keyboard Viewer by a shortcut
- Launch BetterTouchTool
- Add keyboard shortcut
- Add Predefined action >> Open Application/File/Script...
- Navigate to Macintosh HD/System/Library/Input Methods/
- Select KeyboardViewer
Now you have a keyboard shortcut to activate the viewer without an additional script.
HOWEVER, I noticed that pressing the red X button does not actually quit the application, which means that the keyboard viewer will only be activated once. So you need to create a script to quit the program.
To fix this problem :
Close Keyboard Viewer by another keyboard shortcut
- Open Automator
- Choose Application as type of document
- Add RunAppleScript
- Where it says (* Your script goes here *) , replace it with
quit application "KeyboardViewer"
- Compile and save as in documents or anywhere you want ( I moved it to the same location as KeyboardViewer )
- Open BetterTouchTool
- Add keyboard shortcut
- Add Predefined action >> Open Application/File/Script...
- Locate the application file you just saved
You now have a way to quit the KeyBoard viewer for good once its activated. Use it instead of the red X button. Until I figure out why the keyboard viewer does not fully quit when you press the X button, this should be your solution.
EDIT
I have figured out a way to TOGGLE the keyboard viewer !!
TOGGLE KEYBOARD VIEWER by one keyboard shortcut
same process as before but the code is changed :
- Open Automator
- Choose Application as type of document
- Add RunAppleScript
- Where it says (* Your script goes here *) , replace it with
if application "KeyboardViewer" is running then quit application "KeyboardViewer" else activate application "KeyboardViewer" end if
- Compile and save as in documents or anywhere you want ( I moved it to the same location as KeyboardViewer )
- Open BetterTouchTool
- Add keyboard shortcut
- Add Predefined action >> Open Application/File/Script...
- Locate the application file you just saved
Now the Keyboard Viewer is toggled and you dont even need to assign multiple shortcuts !
Link for AppleScript
I uploaded the app online for faster access rather than doing the whole coding steps above (just activate this app through BetterTouchTool) However, since im not an identified publisher, your OS may block running this app, depending on your security preferences. So if you prefer to not run apps from unidentified publishers, just follow the steps above to create your own app !
Link to file : https://www.box.com/s/e2461c91e30e0af025e7
Hope it helps anyone as it helped me :)