Click here to Skip to main content
15,881,579 members
Articles / Programming Languages / C#
Tip/Trick

How to get keyboard language in XNA?

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
28 Jan 2013CPOL 10.6K   1   3
We want to get Keyboard Language and we wonder how?

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.
C#
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:

C#
 CurrentCultureInfo myclsCurrentCultureInfo = new CurrentCultureInfo();
string strCulture = myclsCurrentCultureInfo.InputLangName;

So here we create an object from our class that initialized our current input language.

C#
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer
Italy Italy
Education:

NODET 2003-2007
B.Sc. Electrical Power Engineering 2007–2012

Work:

T.G.D. Co.(Power,Software) July 2010 -February 2012
Artnous Co.(Power) July 2010 -February 2012
Savay Co.(Software) September 2011- July 2012

Comments and Discussions

 
QuestionQuestion? Pin
fjdiewornncalwe28-Jan-13 9:09
professionalfjdiewornncalwe28-Jan-13 9:09 
AnswerRe: Question? Pin
F.moghaddampoor28-Jan-13 13:48
F.moghaddampoor28-Jan-13 13:48 
GeneralRe: Question? Pin
fjdiewornncalwe29-Jan-13 4:05
professionalfjdiewornncalwe29-Jan-13 4:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.