Click here to Skip to main content
15,921,643 members
Articles / Programming Languages / C# 4.0

Changing Keyboard Layout

Rate me:
Please Sign up or sign in to vote.
3.13/5 (12 votes)
29 Jun 2010CDDL 72.8K   4.7K   9   17
Programmatically switch keyboard layout in C#

Introduction

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

C#
[DllImport("user32.dll")] 
private static extern long GetKeyboardLayoutName(System.Text.StringBuilder pwszKLID); 
[DllImport("user32.dll")] 
private static extern long LoadKeyboardLayout(string pwszKLID, uint Flags);

License

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


Written By
Egypt Egypt
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.
This is a Organisation (No members)


Comments and Discussions

 
GeneralMy vote of 1 Pin
yoke14-Sep-12 14:01
yoke14-Sep-12 14:01 
GeneralMy vote of 3 Pin
Burak Tunçbilek11-Aug-12 3:54
Burak Tunçbilek11-Aug-12 3:54 
QuestionFull code that is missing in article Pin
Angelic Care4-Feb-12 13:28
Angelic Care4-Feb-12 13:28 
AnswerRe: Full code that is missing in article Pin
Amer Bakeer12-Jul-14 11:25
Amer Bakeer12-Jul-14 11:25 
QuestionWell worth 5! Pin
JohnStodden24-Jan-12 1:56
JohnStodden24-Jan-12 1:56 
QuestionMy vote of 5 Pin
Member 76951445-Jul-11 22:38
Member 76951445-Jul-11 22:38 
GeneralMy vote of 4 Pin
z4rk15-Jul-10 22:49
z4rk15-Jul-10 22:49 
GeneralRe: My vote of 4 Pin
Abdelmawgoud M. Meabed6-Jul-11 0:45
Abdelmawgoud M. Meabed6-Jul-11 0:45 
GeneralMy vote of 2 Pin
Michael B. Hansen30-Jun-10 2:59
Michael B. Hansen30-Jun-10 2:59 
Not enough details - no explanation of the inner workings, why it would necessary to change keyboard layout and no explanation of the sentence: "The disadvantage of this is that importing external DLLs in the applications slows down the applications on Windows 7"
GeneralMy vote of 1 Pin
karabax29-Jun-10 14:10
karabax29-Jun-10 14:10 
GeneralMy vote of 5 Pin
Member 475607928-Jun-10 19:49
Member 475607928-Jun-10 19:49 
GeneralMy vote of 5 Pin
Abdelmawgoud M. Meabed28-Jun-10 19:05
Abdelmawgoud M. Meabed28-Jun-10 19:05 
GeneralMy vote of 1 Pin
Emilio Garavaglia28-Jun-10 1:28
Emilio Garavaglia28-Jun-10 1:28 
GeneralRe: My vote of 1 Pin
Johnny J.28-Jun-10 21:05
professionalJohnny J.28-Jun-10 21:05 
GeneralRe: My vote of 1 Pin
Emilio Garavaglia28-Jun-10 21:30
Emilio Garavaglia28-Jun-10 21:30 
GeneralNot an article. Pin
R. Giskard Reventlov28-Jun-10 0:54
R. Giskard Reventlov28-Jun-10 0:54 
GeneralRe: Not an article. Pin
Johnny J.28-Jun-10 21:09
professionalJohnny J.28-Jun-10 21: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.