Assigning shortcuts to particular keyboard layouts

Is there any way to assign hotkeys to switching to particular keyboard layouts directly, without having to cycle through them one by one?

I am quite new to OS X. On Windows (which I used before) it is possible to set keyboard shortcuts for witching directly to particular keyboard layouts. For example, I set it up so that Shift-Alt-1 switches directly to English, Shift-Alt-2 switches directly to Hungarian, etc. This is very convenient when one has several layouts installed (I regularly used 4 different ones on Windows), and much faster than having to cycle through layouts one by one, especially when writing multilingual documents and needing to switch often.

Is there similar functionality in OS X? If there's isn't by default, is there a hack or third party program to set it up?


Solution 1:

Command Line Utility + System Service

This is a two-part solution: first a means of changing the keyboard layout, and then a means to activate that by a keyboard shortcut. But it's fairly easily accomplished.

1. Changing the Layout

To change the layout programmatically, there's a handy (and free) little utility called changeInput, which you can download here.

Download and unzip it, and place the changeInput executable somewhere. Be sure to note the path for the second section.

2. Creating Keyboard Shortcuts

There several ways to do this, but to make things accessible for anyone, we'll do it with built-in tools by making a System Service with Automator.

  1. Open Automator, and select Service as the document type.
  2. Change the drop-down menus in the upper right so that it reads Service recieves no input in any application.
  3. Add the Run Shell Script action to the workflow by double-clicking it or dragging it to the right pane.
  4. In the text field, enter /path/to/changeInput "Name of Layout Here". Be sure to enter the exact path to changeInput as you saved it (tip: you can drag the executable right into the text field to get the proper path). Replace "Name of Layout Here" with the exact text that's displayed in the Input Sources section of System Preferences or the Input Menu — be sure to keep the quotes.
  5. Save your service. It will prompt you for a name, make sure to note it for step 7 (something descriptive is best). The service file gets saved to ~/Library/Services/.
  6. Open the Keyboard panel of System Preferences. Click the Keyboard Shortcuts tab, and select the Services item in the left pane.
  7. Scroll through the list until you find the name of the service as you saved it in step 5 (it should be in the General section, near the bottom. Make sure the box is checked, then select it and click add shortcut, and set whatever shortcut you desire.
  8. Repeat steps 1-7 for each layout you want a shortcut for.
  9. Test out your shortcuts. You may need to open the services menu once (click the application menu for the current app and reveal the Services menu) before the shortcuts get activated.

As an alternative to using the Automator/Services combo to create the keyboard shortcuts, you can use FastScripts, the free version of which allows you to quickly set keyboard shortcuts for 10 scripts, or $14.95 for an unlimited number. Keyboard Maestro can also easily run a script with a keyboard shortcut, but it's a bit overkill if this is your only need (it is worth checking out for lots of other scripty/powerusery type things).

Solution 2:

There is a solution that has been vetted on 10.5 through 10.7 and has both a source code example as well as a link to a direct download of a program to do this sort of switching.

https://discussions.apple.com/thread/4032238

You'll need Xcode or to use the compiled program to test if this works for you. And always, use caution when downloading software from the internet. Most people are nice and good, but sometimes it's hard to know what you're running.