Click here to Skip to main content
11,928,569 members (55,942 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


9 bookmarked

Changing Keyboard Layout

, 29 Jun 2010 CDDL
Rate this:
Please Sign up or sign in to vote.
Programmatically switch keyboard layout in C#


In general, developers want to set the layout of keyboard prior to application localized language. In this demonstration, I will develop a simple Windows application with custom control in type of text box. So, we can make the default input language of this text box as property.

Using the Code

The code is separated into two parts, one of them deals with keyboard layout and the other is a client which tests this.

  1. For better performance, get the machine installed language as follows:
    public static InputLanguage GetInputLanguageByName(string inputName) 
        foreach (InputLanguage lang in InputLanguage.InstalledInputLanguages) 
            if (lang.Culture.EnglishName.ToLower().StartsWith(inputName)) 
                return lang; 
        return null; 
  2. Set your preferred language at run time:
    public void SetKeyboardLayout(InputLanguage layout) 
    InputLanguage.CurrentInputLanguage = layout; 

Remember the last used language before leaving the control if you intend to put editing controls with a different language in the same Windows Form.

Points of Interest

In the previous version of changing keyboard layout in this applicatoin, the code below was used.

The disadvantage of this is that importing external DLLs in the applications slows down the applications on Windows 7.

private static extern long GetKeyboardLayoutName(System.Text.StringBuilder pwszKLID); 
private static extern long LoadKeyboardLayout(string pwszKLID, uint Flags);


This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)


About the Author

No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
yoke14-Sep-12 15:01
memberyoke14-Sep-12 15:01 
GeneralMy vote of 3 Pin
Burak Tunçbilek11-Aug-12 4:54
memberBurak Tunçbilek11-Aug-12 4:54 
QuestionFull code that is missing in article Pin
Angelic Care4-Feb-12 14:28
memberAngelic Care4-Feb-12 14:28 
class ActiveLayout
        // For set:
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern bool PostMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
        private static extern long LoadKeyboardLayout(string pwszKLID, uint Flags);
        // For get:

        [DllImport("user32.dll", SetLastError = true)]
        static extern int GetWindowThreadProcessId(
            [In] IntPtr hWnd,
            [Out, Optional] IntPtr lpdwProcessId
        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr GetForegroundWindow();
        [DllImport("user32.dll", SetLastError = true)]
        static extern ushort GetKeyboardLayout(
            [In] int idThread
        static public ushort GetKeyboardLayout() {
            return GetKeyboardLayout(GetWindowThreadProcessId(GetForegroundWindow(), IntPtr.Zero));
        static public long SetKeyboardLayout(String LayoutToLoad) // "00000409" or "00000419"
            long HKL_Layout = LoadKeyboardLayout(LayoutToLoad, 1);  // 00000429
            //Где-то в коде
            //0x0050 - код сообщения WM_INPUTLANGCHANGEREQUEST
            PostMessage(GetForegroundWindow(), 0x0050, 2, 0);   // Layout changed. You think anyone checks TO WHAT?! just switch triggering
            return HKL_Layout;
        public static uint AttemptNo = 0;
        static public int SwitchTo(uint LayoutID) // 0x409 for ENG
            if(ActiveLayout.GetKeyboardLayout() != LayoutID)  // If not english ( 0x409 ) - switch
                bool Found = false;
                foreach (InputLanguage c in InputLanguage.InstalledInputLanguages)
                    if(c.Culture.LCID != LayoutID)
                    Found = true;
                    AttemptNo = 0;
                    return -2;
                    AttemptNo = 0;
                    return -1;
                String HexString = LayoutID.ToString("X");
                HexString = "00000000".Substring(HexString.Length) + HexString;
//              Console.WriteLine(HexString);
                AttemptNo ++;
                AttemptNo = 0;
                return 1;
            return 0;   // Sent request, result not known yet

AnswerRe: Full code that is missing in article Pin
Amer Bakeer12-Jul-14 12:25
memberAmer Bakeer12-Jul-14 12:25 
QuestionWell worth 5! Pin
JohnStodden24-Jan-12 2:56
memberJohnStodden24-Jan-12 2:56 
QuestionMy vote of 5 Pin
Member 76951445-Jul-11 23:38
memberMember 76951445-Jul-11 23:38 
GeneralMy vote of 4 Pin
lmkdev15-Jul-10 23:49
memberlmkdev15-Jul-10 23:49 
GeneralRe: My vote of 4 Pin
Abdelmawgoud M. Meabed6-Jul-11 1:45
memberAbdelmawgoud M. Meabed6-Jul-11 1:45 
GeneralMy vote of 2 Pin
Michael B. Hansen30-Jun-10 3:59
memberMichael B. Hansen30-Jun-10 3:59 
GeneralMy vote of 1 Pin
karabax29-Jun-10 15:10
memberkarabax29-Jun-10 15:10 
GeneralMy vote of 5 Pin
Member 475607928-Jun-10 20:49
memberMember 475607928-Jun-10 20:49 
GeneralMy vote of 5 Pin
abdelmogod28-Jun-10 20:05
memberabdelmogod28-Jun-10 20:05 
GeneralMy vote of 1 Pin
emilio_grv28-Jun-10 2:28
memberemilio_grv28-Jun-10 2:28 
GeneralRe: My vote of 1 Pin
Johnny J.28-Jun-10 22:05
memberJohnny J.28-Jun-10 22:05 
GeneralRe: My vote of 1 Pin
emilio_grv28-Jun-10 22:30
memberemilio_grv28-Jun-10 22:30 
GeneralNot an article. Pin
digital man28-Jun-10 1:54
memberdigital man28-Jun-10 1:54 
GeneralRe: Not an article. Pin
Johnny J.28-Jun-10 22:09
memberJohnny J.28-Jun-10 22:09 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151126.1 | Last Updated 29 Jun 2010
Article Copyright 2010 by Abdelmawgoud M. Meabed
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid