Click here to Skip to main content
Licence CDDL
First Posted 27 Jun 2010
Views 12,368
Downloads 870
Bookmarked 7 times

Changing Keyboard Layout

By | 29 Jun 2010 | Article
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)

About the Author

Abdelmawgoud M. Meabed



Egypt Egypt

Member



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

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionFull code that is missing in article PinmemberAngelic Care13:28 4 Feb '12  
QuestionWell worth 5! PinmemberJohnStodden1:56 24 Jan '12  
QuestionMy vote of 5 PinmemberMember 769514422:38 5 Jul '11  
GeneralMy vote of 4 Pinmemberlmkdev22:49 15 Jul '10  
GeneralRe: My vote of 4 PinmemberAbdelmawgoud M. Meabed0:45 6 Jul '11  
GeneralMy vote of 2 PinmemberMichael B. Hansen2:59 30 Jun '10  
GeneralMy vote of 1 Pinmemberkarabax14:10 29 Jun '10  
GeneralMy vote of 5 PinmemberMember 475607919:49 28 Jun '10  
GeneralMy vote of 5 Pinmemberabdelmogod19:05 28 Jun '10  
GeneralMy vote of 1 Pinmemberemilio_grv1:28 28 Jun '10  
GeneralRe: My vote of 1 PinmemberJohnny J.21:05 28 Jun '10  
GeneralRe: My vote of 1 Pinmemberemilio_grv21:30 28 Jun '10  
GeneralNot an article. Pinmemberdigital man0:54 28 Jun '10  
GeneralRe: Not an article. PinmemberJohnny J.21:09 28 Jun '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.

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