title: Language Bar change language in c# .NET
body: <p><strong>TL;DR</strong>: Trying to solve the issue that <code>InputLanguage</code> changes the input layout but does not update the Language Bar display.</p>
  
  <hr>
  
  <p>I am writing a custom plugin for Trados Studio. Part of this is interfacing with languages both in the app itself and in Word, as in this question: <a href="https://stackoverflow.com/questions/37244656/list-of-all-available-languages-for-windows-net-framework">List of all available languages for Windows .NET framework</a></p>
  
  <p>The last problem I seem to cannot solve is that in a part of the code I am using <code>InputLanguage</code> to set the keyboard input to en-US.</p>
  
  <p><em>To clarify, there is a <strong>limited API,</strong> so I have to be really inventive in automating certain aspects. The best workable approach was to use the default shortcuts in the application:</em></p>
  
  <ol>
  <li><p>First I change the input language to en-US.</p></li>
  <li><p>Then I send some keys to the application.</p></li>
  <li><p>Then I change the input language back to what it was before. </p></li>
  <li><p>Then I show a form.</p></li>
  </ol>
  
  <p>Here is the code:</p>
  
  <pre><code>//change input language to English
  InputLanguage currentLang = InputLanguage.CurrentInputLanguage;
  InputLanguage newLang = InputLanguage.FromCulture(System.Globalization.CultureInfo.GetCultureInfo("en-US"));
  if (newLang == null)
  {
      MessageBox.Show("The Upload Project function requires the En-US keyboard installed.", "Missing keyboard", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
      return;
  }
  else
  {
      InputLanguage.CurrentInputLanguage = newLang;
  }
  
  //Save the document
  SendKeys.SendWait("^s");
  
  InputLanguage.CurrentInputLanguage = currentLang;
  
  //get url and credentials from a custom input form
  string[] psw = UploadData.GetPassword(
      Settings.GetValue("Upload", "Uri", "https://www.scntranslations.org/ws/services"),
      Vars.wsUsername == null ? Settings.GetValue("Upload", "User", "") : Vars.wsUsername,
      Vars.wsPassword == null ? "" : Vars.wsPassword
      );
  Application.DoEvents();
  </code></pre>
  
  <p>The manifestation that I have is that the Language Bar changes with a delay to <strong>EN</strong> but by the time the form shows up it should be <strong>HU</strong> yet it stays <strong>EN</strong>. </p>
  
  <p>However if I test it with <code>Debug.WriteLine(InputLanguage.CurrentInputLanguage.LayoutName)</code> then the output is the correct language (in my case "Hungarian"). </p>
  
  <p>Even after the form is hidden, the language remains <strong>EN</strong>, yet the keyboard types in Hungarian and the <code>Debug.WriteLine(InputLanguage.CurrentInputLanguage.LayoutName)</code> returns "Hungarian".</p>
  
  <p>I looked at dozens of pages on SO and the web and I tried everything I could think of including <code>System.Threading.Thread.Sleep(1000);</code> and <code>Application.DoEvents()</code> and <code>Sendkeys.Flush()</code>, but nothing triggers Windows to update the Language Bar, and I could not find any solution to this issue.</p>
  
  <p>The earlier version of this problem as given in this question: <a href="https://stackoverflow.com/questions/37291533/change-keyboard-layout-from-c-sharp-code-with-net-4-5-2">Change keyboard layout from C# code with .NET 4.5.2</a><br>
  I have made this work now fully with the above implementation of the <code>InputLanguage</code> bar this last hick-up. </p>
  
  <hr>
  
  <p><strong>Can anybody help in:</strong></p>
  
  <ol>
  <li>giving an explanation for this behavior?</li>
  <li>suggesting a solution that triggers the update of the Language Bar from C# code?</li>
  </ol>
  
  <p><strong>UPDATE:</strong> The code now fully works. I implemented the BlockInput WinAPI.</p>
  
  <pre><code>[System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "BlockInput")]
  [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
  public static extern bool BlockInput([System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)] bool fBlockIt);
  </code></pre>
  
  <p>And also turning off the Caps Lock using Win API:</p>
  
  <pre><code>[DllImport("user32.dll")]
  public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);
  
  private void TurnOffCapsLock()
  {
      if (Control.IsKeyLocked(Keys.CapsLock)) // Checks Capslock is on
      {
          const int KEYEVENTF_EXTENDEDKEY = 0x1;
          const int KEYEVENTF_KEYUP = 0x2;
          NativeMethods.keybd_event(0x14, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr)0);
          NativeMethods.keybd_event(0x14, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
          (UIntPtr)0);
      }
  }
  </code></pre>
  
  <p><em>So what's left really is getting the Language Bar to correctly show the actual current input language.</em></p>
  
  <p>I do have the option of sending Alt+Shifts, but I really don't want to do that.</p>