Detecting current keyboard language / layout name on multilingual computer

I am trying to develop an application in C # that is required to detect a user selected language (keyboard layout). However, there are two languages ​​installed on my computer, the code always returns to default, even I change the language before running the application.

InputLanguage myCurrentLanguage = InputLanguage.InstalledInputLanguages[1]; // here I can see collection of languages 
InputLanguage myCurrentLanguage2 = InputLanguage.CurrentInputLanguage; // always return first or default one

      

Is there any method for determining the real selected / running language?

+6


source to share


6 answers


Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName

in namespace System.Threading

returns what you call the default culture and is not related to keyboard layout. That is, on my computer, this returns "de"

, the culture I use to format the date and number. However, I am using the US-ASCII keyboard and .Culture.Name

and .LayoutName

from System.Windows.Forms.InputLanguage.CurrentInputLanguage

return "en-US"

and "US"

respectively.

Thread.CurrentThread.CurrentCulture

It gives a lot of additional information, for example KeyboardLayoutId

, DisplayName

(the localized name of the culture) EnglishName

, DateTimeFormat

etc.


I did some tests and noticed strange behavior. I displayed the Windows language bar and selected the secondary input language. But whenever I ran the small test-WinForms application, the input language automatically switched to the default language. After the application was launched, I switched the input language to secondary. Now he stayed before that.



In both cases, this gave me the correct input language (the one displayed in the language bar):

lblInputLanguage.Text = InputLanguage.CurrentInputLanguage.Culture.Name;
lblKeyboardLayout.Text = InputLanguage.CurrentInputLanguage.LayoutName;

      

This thread on superuser may shed some light on the problem: How to avoid keyboard layouts automatically changing in windows

+7


source


This one returns the currently active input language (the one that currently applies to your keyboard types):



InputLanguage myCurrentLang = InputLanguage.CurrentInputLanguage;

      

+3


source


You can get this by calling the GetKeyboardLayout Windows API function .

Stream keyboard layout.

I don't know if there is a built-in managed version of this feature.

+1


source


For WPF (instead of Windows Forms) has System.Windows.Input.InputLanguageManager, which has InputLanguageManager.Current.AvailableInputLanguages

and InputLanguageManager.Current.CurrentInputLanguage

etc.

0


source


You must uncheck the setting let me set a different input method for each app window

Settings/Devices/Typing/Advanced Keyboard

to

string lang = InputLanguage.CurrentInputLanguage.Culture.Name;

      

work. If you do, it will return the current language layout you are using for input, even if it is different from the default keyboard language.

0


source


string StrCurrentLang = InputLanguage.CurrentInputLanguage.Culture.TwoLetterISOLanguageName;

      

line up code, you will get the last two letters in the current language

on the other hand you can change the current language using the paragraph code down

    foreach (InputLanguage lang in InputLanguage.InstalledInputLanguages)
    {

        if (lang.Culture.TwoLetterISOLanguageName != StrCurrentLang )
        {
            InputLanguage.CurrentInputLanguage = lang;
            return;
        }
    }

      

0


source







All Articles