Click here to Skip to main content
15,441,508 members
Articles / Programming Languages / C#
Tip/Trick
Posted 28 Jan 2013

Tagged as

Stats

9.8K views
1 bookmarked

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
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
MemberF.moghaddampoor28-Jan-13 13:48 
Hello Marcus, yes there is a big difference, system.globalization doesn't work many times, its because the thread that is running is using application local language, well if some one install a language out of the application in windows you may not reach it through system.globalization, i tried this in my system and all of UICulture and CurrentCulture was giving me en-US however i changed my keyboard layout to Persian language input.
My code works.
And i have a question from you. why didn't i get notification of your question in my notification toolbar, it's strange for me that i don't get important notifications.

I used the namespace you mentioned to store not to use it independently.
I initialized it like this:

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;
}
Even i should say i tried Microsoft code:

http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.currentuiculture.aspx[^]
but it didn't work for me.

modified 28-Jan-13 20:05pm.

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.