28

In Mac OS X, there is a keyboard shortcuts to switch between activated input sources.

Alt text

Is it possible to switch to a specific language? So if you have three input methods, you could have three shortcuts: Ctrl + 1 for English, Ctrl + 2 for Spanish and Ctrl + 3 for Russian.

Nikem
  • 403

9 Answers9

12

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".

Asmus
  • 2,147
7

I found kawa. It is a quite simple application to exactly fulfill your needs.

It sets up shortcuts to directly switch to the needed input source.

7

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.

Daniel Beck
  • 111,893
6

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.

Lri
  • 42,502
  • 8
  • 126
  • 159
3

But even in the end of 2016, I still didn't find a simple and nice solution. Therefore, I made IMEShortcuts, and it works on macOS v10.12 (Sierra).

You can assign a customized shortcut for any specific input method. Please give it a shot. And if you guys find any bugs, I would happy to help.

Also, Karabiner (known as KeyRemap4MacBook) is a great keyboard customizer for Mac. But it's a little bit complicated and currently broken on Sierra. If you're using Mac OS X v10.10 (Yosemite) or Mac OS X v10.11 (El Capitan), it would be another nice choice.

rock9c
  • 31
1

I use im-select, and you could find it here from GitHub.

It's a command line tool that you can easily integrate it to a lot of other tools you want. The command to switch input method is simple, e.g.,

im-select com.apple.keylayout.US

will switch input method to English (U.S.).

I then integrate these commands into BetterTouchTool, like this,

Enter image description here

j5shi
  • 141
1

I have another solution that has satisfied me the most. KeyRemap4MacBook is application that allows you to remap your keyboard buttons. Among other things it allows for some, not at all exhaustive, support for language change. So we some custom configuration it does what I want.

Cons: - third party application - only limited language support for now

Pros: - Conceptually a little bit nicer that running script on UI

Nikem
  • 403
0

On AppleScript you must only take cmd + "space" (or something other, what you change keyboard source).

And all what you need:

    key code 49 using command down

49 - code of 'space' button in ASCII for AppleScript.

P.S.: don't forget get access for you AppleScript utility in System Preferences.

-1

This link has a video on how to do it. It already has a hotcake with is "command + space", but the spotlight hot-key is also "command + space". Spotlight over-riders the hot-key, so the language switch doesn't occur. The video in the link will show you how to change the hot-key so that you can toggle the hot-key.

Good Luck,

David
  • 7,393