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

Tagged as

Go to top

Changing Keyboard Layout

, 29 Jun 2010
Rate this:
Please Sign up or sign in to vote.
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:
    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.

[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)

Share

About the Author

No Biography provided

Comments and Discussions

 
GeneralMy vote of 1 Pinmemberyoke14-Sep-12 14:01 
GeneralMy vote of 3 PinmemberBurak Tunçbilek11-Aug-12 3:54 
QuestionFull code that is missing in article PinmemberAngelic Care4-Feb-12 13:28 
AnswerRe: Full code that is missing in article PinmemberAmer Bakeer12-Jul-14 11:25 
QuestionWell worth 5! PinmemberJohnStodden24-Jan-12 1:56 
QuestionMy vote of 5 PinmemberMember 76951445-Jul-11 22:38 
GeneralMy vote of 4 Pinmemberlmkdev15-Jul-10 22:49 
GeneralRe: My vote of 4 PinmemberAbdelmawgoud M. Meabed6-Jul-11 0:45 
GeneralMy vote of 2 PinmemberMichael B. Hansen30-Jun-10 2:59 
GeneralMy vote of 1 Pinmemberkarabax29-Jun-10 14:10 

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
Web02 | 2.8.140916.1 | Last Updated 29 Jun 2010
Article Copyright 2010 by Abdelmawgoud M. Meabed
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid