Introduction
Hello friends, wknowing how to get keyboard language may be very boring for thos who want a fast answer. Well there are lots of ways to do it, but the fastest and more reliable
code is to use Windows APIs.
So let's go get it.
Using the code
First we create a class and put this code in it.
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Globalization;
using System.Windows.Forms;
namespace Language_Learning_Application
{
public class CurrentCultureInfo
{
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
private static extern int GetWindowThreadProcessId(IntPtr handleWindow, out int lpdwProcessID);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
private static extern IntPtr GetKeyboardLayout(int WindowsThreadProcessID);
public CurrentCultureInfo()
{
IntPtr hWnd = GetForegroundWindow();
int WinThreadProcId = GetWindowThreadProcessId(hWnd, out lpdwProcessId);
IntPtr KeybLayout = GetKeyboardLayout(WinThreadProcId);
for (int i = 0; i < installedInputLanguages.Count; i++)
{
if (KeybLayout == installedInputLanguages[i].Handle)
currentInputLanguage = installedInputLanguages[i].Culture;
}
}
#region Fields & Properties
private int lpdwProcessId;
private InputLanguageCollection installedInputLanguages = InputLanguage.InstalledInputLanguages;
private CultureInfo currentInputLanguage;
public string InputLangTwoLetterISOLanguageName
{
get { return currentInputLanguage.TwoLetterISOLanguageName; }
}
public string InputLangThreeLetterWindowsLanguageName
{
get { return currentInputLanguage.ThreeLetterWindowsLanguageName; }
}
public string InputLangThreeLetterISOLanguageName
{
get { return currentInputLanguage.ThreeLetterISOLanguageName; }
}
public string InputLangNativeName
{
get { return currentInputLanguage.NativeName; }
}
public string InputLangName
{
get { return currentInputLanguage.Name; }
}
public int InputLangLCID
{
get { return currentInputLanguage.LCID; }
}
public int InputLangKeyboardLayoutId
{
get { return currentInputLanguage.KeyboardLayoutId; }
}
public string InputLangEnglishName
{
get { return currentInputLanguage.EnglishName; }
}
public string InputLangDisplayName
{
get { return currentInputLanguage.DisplayName; }
}
#endregion
}
}
We used Windows DLLs to reach our keyboard layout. Be sure to add the references you need above. Now wherever you need to know the keyboard language, put the code below:
CurrentCultureInfo myclsCurrentCultureInfo = new CurrentCultureInfo();
string strCulture = myclsCurrentCultureInfo.InputLangName;
So here we create an object from our class that initialized our current input language.
public CurrentCultureInfo()
{
IntPtr hWnd = GetForegroundWindow();
int WinThreadProcId = GetWindowThreadProcessId(hWnd, out lpdwProcessId);
IntPtr KeybLayout = GetKeyboardLayout(WinThreadProcId);
for (int i = 0; i < installedInputLanguages.Count; i++)
{
if (KeybLayout == installedInputLanguages[i].Handle) currentInputLanguage = installedInputLanguages[i].Culture;
}
}
Well, it was easy, Thank you for your attention.
Points of Interest
To create a multilanguage application you need to know the Windows current language, here was a very easy and not boring way we did it.
History
Started at 1/28/2013.