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

Tagged as

Go to top

How to get keyboard language in XNA?

, 28 Jan 2013
Rate this:
Please Sign up or sign in to vote.
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.
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)

Share

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

Comments and Discussions

 
QuestionQuestion? PinmvpMarcus Kramer28-Jan-13 9:09 
AnswerRe: Question? [modified] PinmemberF.moghaddampoor28-Jan-13 13:48 
GeneralRe: Question? PinmvpMarcus Kramer29-Jan-13 4:05 

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

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

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