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?
source to share
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
source to share
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.
source to share
For WPF (instead of Windows Forms) has System.Windows.Input.InputLanguageManager, which has InputLanguageManager.Current.AvailableInputLanguages
and InputLanguageManager.Current.CurrentInputLanguage
etc.
source to share
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.
source to share
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;
}
}
source to share