1

Please look at the code below to better understand what exactly I mean:

using System;
using System.Windows.Forms;

namespace CurrentInputLanguageTest
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Console.WriteLine(InputLanguage.CurrentInputLanguage.LayoutName); // It's US
            Console.ReadLine(); // Changed my keyboard layout while typing something
            Console.WriteLine(InputLanguage.CurrentInputLanguage.LayoutName); // It's still US

            var form = new Form();
            var button = new Button();
            button.Click += CheckInputLanguage;
            form.Controls.Add(button);
            Application.Run(form);
        }

        static void CheckInputLanguage(object sender, EventArgs e)
        {
            // I have changed my input language while the form is opened and pressed the button.
            // It changes when called in this event handler.
            Console.WriteLine(InputLanguage.CurrentInputLanguage.LayoutName); 
        }
    }
}

The problem: I have some event handler in my app and I need to know what is current input language when the event is fired. How can I do this?

Vladyslav
  • 786
  • 4
  • 19

1 Answers1

0

The reason it always returns US because you are inside the command and only for that input line is the setting changed.

If the user changes the input language while the form is shown, the current language is properly returned. But you can also programmatically set is with another button:

using System;
using System.Windows.Forms;

namespace CurrentInputLanguageTest
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Console.WriteLine(InputLanguage.CurrentInputLanguage.LayoutName); // It's US
            Console.ReadLine(); // Changed my keyboard layout while typing something
            Console.WriteLine(Application.CurrentInputLanguage.LayoutName); // It's still US

            var form = new Form();
            var grid = new TableLayoutPanel();
            var button = new Button();
            var button2 = new Button();
            button2.Left = button.Right + 5;
            form.Refresh();
            button.Click += CheckInputLanguage;
            button2.Click += ChangeInputLanguage;
            form.Controls.Add(button);
            form.Controls.Add(button2);
            Application.Run(form);
        }

        private static void ChangeInputLanguage(object sender, EventArgs e)
        {
            InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(System.Globalization.CultureInfo.GetCultureInfo("en-US"));
        }

        static void CheckInputLanguage(object sender, EventArgs e)
        {
            // I have changed my input language while the form is opened and pressed the button.
            // It changes when called in this event handler.
            Console.WriteLine(InputLanguage.CurrentInputLanguage.LayoutName);
        }
    }
}
ib11
  • 2,530
  • 3
  • 22
  • 55