title: How to programmatically switch an input method on OS X
body: <p>I'm trying to write a program that switches an input method on OS X. There is no problem with normal layouts. E.g. this code works (stripped away CFReleases/CFRetains for brevity):</p>
  
  <pre><code>TISInputSourceRef getref(char* id) {
      CFStringRef name     = CFStringCreateWithCString(kCFAllocatorDefault, id, kCFStringEncodingUTF8);
      CFStringRef keys[]   = { kTISPropertyLocalizedName };
      CFStringRef values[] = { name };
      CFDictionaryRef dict = CFDictionaryCreate(kCFAllocatorDefault, (const void **)keys, (const void **)values, 1, NULL, NULL);
      CFArrayRef array     = TISCreateInputSourceList(dict, true);
      return (TISInputSourceRef) CFArrayGetValueAtIndex(array, 0);
  }
  
  int main() {
      TISSelectInputSource(getref("U.S. International - PC"));
  }
  </code></pre>
  
  <p>However when I select an input method instead of the layout:</p>
  
  <pre><code>TISSelectInputSource(getref("Hiragana"));
  </code></pre>
  
  <p>strange things happen: the IM is selected and visualized correctly in the menubar, however it is not enabled (typing produces Roman letters instead of Japanese). If I then switch to any other app and back the IM starts working correctly. The docs don't mention anything specific for this case. Please help me fix it and thanks in advance.</p>