Switch TO specific input source
I came up with a bit nicer solution in AppleScript, given you know the name of the Keyboard Layout you want to switch to. Create a function like this:
on changeKeyboardLayout(layoutName)
tell application "System Events" to tell process "SystemUIServer"
tell (1st menu bar item of menu bar 1 whose description is "text input") to {click, click (menu 1's menu item layoutName)}
end tell
end changeKeyboardLayout
and then call it by
changeKeyboardLayout("English")
changeKeyboardLayout("German")
Be aware that the names of Keyboard Layouts are localized, i.e. on a german system the above example would need to call "Englisch" and "Deutsch".
I present to you, the ugliest possible "solution":
- Enable support for assistive devices in System Preferences » Universal Access,
- and displaying the Input Sources menu in the menubar in System Preferences » Language and Text.
- Make sure the shortcut
Ctrl-F8
is defined for Move focus to status menus in System Preferences » Keyboard » Keyboard Shortcuts » Keyboard & Text Input. - Make sure the Input Sources menu is the leftmost menu item that can be moved by dragging while holding
Cmd
down.
Use AppleScript Editor
and write three scripts, each of them with the following code:
tell application "System Events"
key code 100 using control down # press Ctrl-F8
delay 0.5 # wait a bit, UI might be slow
key code 125 # press down to open the menu
keystroke "german" # name of your desired language, in my case tested using German
key code 36 # press enter
end tell
Save once for each language, switching out the name of the language. If you want to press different keys, or assign something different from Ctrl-F8
, substitute with the key codes here. You can also move the Input Sources menu from its leftmost position by inserting a few right arrow key presses.
Invoke scripts however you want, e.g. use your application launcher (Quicksilver, Launchbar etc.), or wrap them in Services using Automator, and assign them keyboard shortcuts in System Preferences » Keyboard » Keyboard Shortcuts » Services.
One option would be to download changeInput and assign shortcuts to shell commands like changeInput U.S.
.
You could also use KeyRemap4MacBook:
<?xml version="1.0"?>
<root>
<vkchangeinputsourcedef>
<name>KeyCode::VK_CHANGE_INPUTSOURCE_HIRAGANA</name>
<inputsourceid_equal>com.apple.inputmethod.Kotoeri.Japanese</inputsourceid_equal>
</vkchangeinputsourcedef>
<item>
<name>change_inputsources</name>
<identifier>change_inputsources</identifier>
<autogen>__KeyToKey__ KeyCode::E, VK_OPTION | ModifierFlag::NONE, KeyCode::VK_CHANGE_INPUTSOURCE_ENGLISH</autogen>
<autogen>__KeyToKey__ KeyCode::H, VK_OPTION | ModifierFlag::NONE, KeyCode::VK_CHANGE_INPUTSOURCE_HIRAGANA</autogen>
</item>
</root>
See the private.xml documentation.
VK_CHANGE_INPUTSOURCE_ENGLISH is defined in vkchangeinputsourcedef.xml. You can see the IDs of input sources from EventViewer.app. Without | ModifierFlag::NONE
for example the first setting would also apply to option-command-E. See the source for the key code values and predefined settings.