Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I changed the shortcut to Command ⌘ + X to change the language of the keyboard.

However I would like to display the Keyboard Viewer using a shortcut as well.

  • 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?
share|improve this question
    
The ideal solution would be to assign a keyboard shortcut to an Apple Script which shows the Keyboard. –  duci9y Aug 30 '12 at 16:50

6 Answers 6

you can do everything by Automator:

  1. open Automator and select "Application"
  2. in top of Library find "Launch application", double click it
  3. choose "other" and navigate to KeyboardViewer file in /System/Library/Input Methods
  4. Save with an name like "start keyboard". test it.

next part is to close keyboard:

  1. open Automator and select "Application"
  2. in top of Library find "Quit application", double click it
  3. choose "other" and navigate to KeyboardViewer file in /System/Library/Input Methods

  4. Save with an name like "quit keyboard". test it.

now you will have two files one for open, one for close.

all you need is associate your mouse buttons to launch these two files!

for example, i`m using Microsoft Bluetooth Notebook Mouse 5000 and it

work great for me in 10.9.1

share|improve this answer

This would also work after the main window has been closed by clicking the close button:

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
        launch application "KeyboardViewer"
    else
        quit application "KeyboardViewer"
    end if
else
    launch application "KeyboardViewer"
end if

If Keyboard Viewer is opened with AppleScript or open -a KeyboardViewer and the main window is closed, the process stays open and keeps using about 0-15% CPU. It doesn't happen when selecting Show Keyboard Viewer from the input menu. So a better option might be to click the menu item:

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

Both scripts require access for assistive devices to be enabled in the accessibility preference pane.

share|improve this answer

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.

share|improve this answer

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.
share|improve this answer
    
YES! This is exactly for what I looking. I'm familiar with automator and shell-scripting, only needed a hint. Thank you very much... :) –  jm666 Aug 30 '12 at 17:38
    
I updated this using an Applescript that runs a check to see if the window has been closed, and if so, quits the process, to avoid the performance drain. Anyone interested in the old bash-based version can find that revision here. –  robmathers Sep 4 '12 at 17:28

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.

share|improve this answer
    
I'm usually don't like install 3rd party apps for simple scriptable things, but this is the exception. :) Thank you very much, working like a charm. ;) –  jm666 Aug 30 '12 at 17:25
    
If you want to avoid 3rd party software, I just posted an answer that will do the same thing with built-in software. –  robmathers Aug 30 '12 at 17:33
    
sry mach, @CanuckSkier posted an answer what i like more, so accepting his answer. +1 anyway for the QuickSilver. ;) –  jm666 Aug 30 '12 at 17:40
up vote 2 down vote accepted

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 :)

share|improve this answer
    
fantastic!! I thought adding the keyboardViewer app directly to my launchbar index would be a good substitute for my purposes, but there's no option to close kbv once it's running! so, you can't invoke it a second time - weird. anyway your script works a treat! (I had to save it as a script/run only for it to work for me.) Tx! –  memeLab Mar 15 '13 at 10:27
    
@memeLab Glad it worked for you :) –  Render Mar 15 '13 at 13:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.