Click here to Skip to main content
Click here to Skip to main content

How to get keyboard language in XNA?

By , 28 Jan 2013
 

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.

License

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

About the Author

F.moghaddampoor
Engineer
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionQuestion?mvpMarcus Kramer28-Jan-13 9:09 
AnswerRe: Question? [modified]memberF.moghaddampoor28-Jan-13 13:48 
GeneralRe: Question?mvpMarcus Kramer29-Jan-13 4:05 

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130619.1 | Last Updated 28 Jan 2013
Article Copyright 2013 by F.moghaddampoor
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid