How to make keyboard viewer to automatically show up only when I’m holding the option key

character-mapkeyboard

I'd like keyboard viewer to show up while I hold the option key or after pressing a "dead key" such as option-e, but to hide otherwise.

I know where the letters are. But it would be nice to get an automatic reminder of how to type special characters, especially since my keyboard layout (known as "qwpr") allows typing hundreds of special glyphs.

Best Answer

You could use a private.xml like this with KeyRemap4MacBook:

<root>
  <vkopenurldef>
    <name>KeyCode::VK_OPEN_URL_KEYBOARDVIEWER</name>
    <url>file:///System/Library/Input%20Methods/KeyboardViewer.app</url>
  </vkopenurldef>
  <vkopenurldef>
    <name>KeyCode::VK_OPEN_URL_QUITKEYBOARDVIEWER</name>
    <url>file:///Applications/QuitKeyboardViewer.app</url>
  </vkopenurldef>
  <item>
    <name>test</name>
    <identifier>test</identifier>
    <autogen>
    __KeyToKey__ KeyCode::OPTION_L,
    ModifierFlag::OPTION_L | ModifierFlag::NONE,
    KeyCode::OPTION_L,
    Option::KEYTOKEY_BEFORE_KEYDOWN,
    KeyCode::VK_OPEN_URL_KEYBOARDVIEWER,
    Option::KEYTOKEY_AFTER_KEYUP,
    KeyCode::VK_OPEN_URL_QUITKEYBOARDVIEWER
    </autogen>
    <autogen>
    __KeyToKey__ KeyCode::OPTION_R,
    ModifierFlag::OPTION_R | ModifierFlag::NONE,
    KeyCode::OPTION_R,
    Option::KEYTOKEY_BEFORE_KEYDOWN,
    KeyCode::VK_OPEN_URL_KEYBOARDVIEWER,
    Option::KEYTOKEY_AFTER_KEYUP,
    KeyCode::VK_OPEN_URL_QUITKEYBOARDVIEWER
    </autogen>
  </item>
</root>

Save this script as /Applications/QuitKeyboardViewer.app in AppleScript Editor:

quit application "KeyboardViewer"

There is a noticeable (but not that long) delay before KeyboardViewer is opened or closed. If for example you hold option and command simultaneously, KeyboardViewer is shown if you press option first but not if you press command first.