34

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?
2
  • The ideal solution would be to assign a keyboard shortcut to an Apple Script which shows the Keyboard.
    – duci9y
    Commented Aug 30, 2012 at 16:50
  • I found this way in Monterey: System Preferences > Accessibility > Keyboard > Viwer - enable; in "Options..." select a HotCorner to show/hide and reduce the Dwell time to 0,25 sec. Then it seems to work well enough with mouse pointer in the screen corner.
    – magnump0
    Commented Dec 29, 2021 at 13:54

11 Answers 11

18

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.
6
  • YES! This is exactly for what I looking. I'm familiar with automator and shell-scripting, only needed a hint. Thank you very much... :)
    – clt60
    Commented Aug 30, 2012 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
    Commented Sep 4, 2012 at 17:28
  • 1
    With minimal modification, this script toggles the viewer. Replace "end if" with "else" and move "else if" to the end.
    – AdamAL
    Commented Feb 18, 2016 at 15:11
  • 11
    /System/Library/Input Methods/KeyboardViewer.app doesn't exist on Catalina. I opened all of the applications in /System/Library/Input Methods and none of them opened the keyboard viewer. Any ideas? Commented Jan 2, 2020 at 10:31
  • 2
    Any updates for the Monterey? I can't setup any solution there... :(
    – magnump0
    Commented Dec 29, 2021 at 13:46
6

It's dead in Catalina - no app called KeyboardViewer any longer.

Instead, use the Accessibility options in System Preferences and toggle everything else off so the Accessibility options shortcut (⌘ + ⎇ + F5) only turns the Accessibility Keyboard on and off. Of course, if you need the other options and to see the keyboard, you're boned. Thanks Apple!!

Accessibility options with only Keyboard selected

1
  • This is an excellent answer using native controls in modern macOS, unless you need other Accessibility Features to work with the command-option-F5 shortcut. Works as of macOS 15.1. Commented Nov 27 at 22:10
5

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

3
  • 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!
    – ptim
    Commented Mar 15, 2013 at 10:27
  • @memeLab Glad it worked for you :)
    – Render
    Commented Mar 15, 2013 at 13:12
  • This is the approach I had to take to get the behavior I wanted. Thanks for the answer. I’m amazed to this day that BTT still doesn’t have a built-in action that offers toggle capabilities. I always have to do a script like this or some funky/hacky thing with a stored variable to achieve toggle functionality using the same keyboard shortcut.
    – Art Geigel
    Commented Jan 1, 2022 at 16:23
5

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.

2
  • 1
    Broken link. Would you be so kind as to post it a again. This is an Alfred Workflow I would use.
    – ddaa
    Commented Oct 24, 2017 at 15:15
  • Here we go, I reimplemented it (and also added a keyboard shortcut Cmd+Alt+Ctrl+Shift+K): packal.org/workflow/toggle-keyboard-viewer-0 Commented Apr 16, 2018 at 19:13
4

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.

4
  • 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. ;)
    – clt60
    Commented Aug 30, 2012 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
    Commented Aug 30, 2012 at 17:33
  • sry mach, @CanuckSkier posted an answer what i like more, so accepting his answer. +1 anyway for the QuickSilver. ;)
    – clt60
    Commented Aug 30, 2012 at 17:40
  • This script is not working for me on Catalina.
    – Gavin
    Commented Jun 19, 2020 at 12:45
3

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.

0
3

The selected answer does not work on Catalina. This one does:

activate application "System Preferences"
tell application "System Preferences"
    reveal anchor "Virtual_Keyboard" in pane id "com.apple.preference.universalaccess"
end tell

tell application "System Events"
    tell process "System Preferences"
        delay 1
        set theCheckbox to checkbox "Enable Accessibility Keyboard" of tab group 1 of group 1 of window 1
        if (value of theCheckbox as boolean) then
            click theCheckbox
            key code 36 -- Handle the confirmation dialog
        else
            click theCheckbox
        end if
        delay 1
    end tell
end tell
tell application "System Preferences" to quit
1
  • Using Keyboard Maestro the keyboard viewer can be toggled by automating a click on the menu item. This skips the control panel and is much faster.
    – Gavin
    Commented Jun 19, 2020 at 14:01
2

It's not a single-stroke shortcut, but a "non-mousing" solution is Fn+Ctrl+F8 to set focus on the "Status menu" icons on the right side of the menu bar. (Section 4)

enter image description here

Arrows will navigate you to the Keyboard Preferences icon and the Show/Hide options. When you enter Fn+Ctrl+F8 the icon to the furthest left side of the "Status menu" icons which are on the right half of the menu bar will get highlighted. Once it is selected, then use the Right Arrow → to navigate to the one you want. Use the Down Arrow ↓ to open the menu and select the option you want. Note that the Option key will make different menu options available in some of the "status menus".

enter image description here

Of note, Fn+Ctrl+F2 will activate the Apple menu icon  on the left hand side. From there the Right Arrow → will give you access to the Application menu. These options are available in System Preferences >> Keyboard >> Shortcuts::Keyboard:

enter image description here

Depending on how you have your Function keys configured, the Fn is optional.

enter image description here

In macOS Sierra you can also use Siri to help with this. Invoke Siri with whichever keyboard shortcut you choose from System Preferences >> Siri::Keyboard Shortcut, for example Option+Spacebar

enter image description here

...then say "Turn on the Keyboard Viewer"

enter image description here

...and voila:

enter image description here

Unfortunately, Siri will not close the Keyboard Viewer, nor will Command⌘+w

1

It is not a shortcut, but working for me. First click on Show keyboard viewer. After when you go with the mouse to the corner, A little triangle showing. When it is ready, the keyboard viewer hide. The next it is showing.

Here is a little video about it: https://dai.ly/x7t7x60

2
  • 1
    The whole point of this question is how to do it Without the mouse. Commented Apr 10, 2020 at 17:19
  • @TomGewecke is correct, of course. To me, this is still a solution that solves my pain, so kudos to Ámon.
    – Robbotnik
    Commented Apr 6, 2023 at 2:32
0

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

0

Activity Monitor, KeyboardViewer.app launches from

Finder location: /System/Library/Input Methods/KeyboardViewer.app.

Normal use: open from Menu Bar, close from mouse hover to view window buttons where (x) close button can be clicked. To enable Menu Bar panel, System Preferences, Keyboard, Keyboard tab, select Show Keyboard and Emoji Viewers in menu bar.

KeyboardViewer applet exhibits restrictive behaviors.

  1. Applet window is not responsive to ⌘-Q, ⌘-W.
  2. Oddly, when opened from folder Input Methods, then Activity Monitor shows that the applet remains running after its window is closed.
  3. Automator AppleScript shown below runs slowly in background, keyboard services take seconds to register open and close actions in Activity Monitor.
  4. Keyboard shortcut script shown below runs without error but system appears to ignore.
  5. Sierra Automator's Quit Application will not close KeyboardViewer.app

The following Automator script can be saved as an application to open-close KeyboardViewer.app with a single click (Activity Monitor also shows this open-close activity), and it works well when placed in Dock.

Automator: New, Application, Run AppleScript, save this script

-- see above post, Render
    on run {input, parameters}
        if application "KeyboardViewer" is running then
            quit application "KeyboardViewer"
        else 
            activate application "KeyboardViewer"
        end if
        return input
    end run
-- icon /System/Library/PreferencePanes Keyboard.prefPane/Contents/Resources/Keyboard.icns

Unfortunately, this does not add a keyboard shortcut. However, it does offer a one-click workaround. There may be some way to add a keyboard shortcut using Automator. A general shortcut script looks something like this (but not sure how to incorporate).

tell application "System Events"
    set application "KeyboardViewer" to keystroke "K" using control down
end tell

(macOS Sierra 10.12.6 on MP 6,1)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .