[
Advertise | Submit Code | About us | Contact us | Link us
]
Go!
Membership Services
Login
Register

Home
C# General

General

C# Language

Design & Architecture

Algorithms

Database

Security

Active Directory

COM Interop

Remoting
C# Windows Forms

General

Combo and List boxes

Miscellaneous Controls

Button Controls

Edit Controls
Cutting Edge

ASP.NET 2.0

Visual Studio 2005

Windows Longhorn

SQL Server 2005
C# Multimedia and GDI+

General

DirectX

GDI+

Audio
Internet & Web

General

Images and multimedia

Database

Utilities

Security

ASP.NET Controls

Design and Architecture

Webservices
.NET

General

Design & Architecture

Algorithms

Database

Security

Active Directory

COM Interop

Remoting

ADO.NET

XML.NET

Tools

Enterprise

IDE
Visual Basic .NET

VB.NET General

VB.NET Controls
General Reading

.NET Books Review

Product Showcase

Book Chapters

Business Design & Strategy
Community

Discuss

Job Board

Discussion

CodeXchange
DeveloperLand

Advertise

Submit Code

About us

Contact us

Link us
Miscellaneous

Favorite Links

Downloads

Programming Sites

Top Stories
Regular Expressions

E-Mail

Date/Time
Home > C# General > General
KeyBoardLayout and Language Selector
Posted by on Wednesday, September 15, 2004 (EST)

This article shows you how to write a Win-Form that will select the language OS automatically.

This article has been viewed: 6,229 times
Technology: General.

TestLanguage.zip (12.03 KB)

Contents

Introduction

I think that for most of the non English-native users, Windows OS is installed with at least 2 languages. Changing the language manually is done using ALT+CTRL, but what if you want to write a Win-Form that will select the language automatically? How to select keyboard layout? Keep reading to find out.

Top Go to Table of Contents

Working with windows API

Please read C# And API's article num.79 [^].

In this article I will demonstrate how use:

Other resources

Identifier

Language

0x0000

Language Neutral

0x0409

English (United States)

0x040d

Hebrew

Top Go to Table of Contents

Sample code

OK, now that we’ve got the knowledge, let’s start working.

In this demo code I will show how to create a LanguageSelector DLL.

  1. Create a new C# Class Library project, name it LanguageSelector.
  2. add :
    using System.Runtime.InteropServices;
  3. add the following constants:
    const uint KLF_ACTIVATE = 1; //activate the layout
    const int  KL_NAMELENGTH = 9; // length of the keyboard buffer
    const string LANG_EN_US = "00000409";
    const string LANG_HE_IL = "0000040d";
  4. Next import the LoadKeyboardLayout function from the “user32.dll”
    [DllImport("user32.dll")]
    private static extern long LoadKeyboardLayout(
          string pwszKLID,  // input locale identifier
          uint Flags         // input locale identifier options
          );
  5. The same import for the GetKeyboardLayoutName:
    [DllImport("user32.dll")]
    //[out] string that receives the name of the locale identifier
    private static extern long GetKeyboardLayoutName(
          System.Text.StringBuilder pwszKLID  
          ); 
  • Please notice the StringBuilder – one could use [MarshelAs…] property instead of the StringBuilder. For the simplicity of the code and not dealing with Managed / Unmanaged code, I didn’t use the marshaling system. 

Top Go to Table of Contents

Adding functionality

Top Go to Table of Contents

Retrieve current keyboard layout name

The code below warps the GetKeyboardLayoutName DLL function into more convenient method, easy to use later on.

public static string getName()
{
      System.Text.StringBuilder name = new System.Text.StringBuilder(KL_NAMELENGTH);
      GetKeyboardLayoutName(name);
      return name.ToString();
}

Tip: One should use the above function in order to discover the installed languages on the target machine.

Top Go to Table of Contents

Changing the keyboard layout

The next two functions force the keyboard layout to be changed. On my machine the Hebrew and English layouts are installed, one may add additional languages upon demand, using the appropriate language identifier.

public static void Hebrew()
{
      //load and activate the layout for the current thread
      LoadKeyboardLayout(LANG_HE_IL, KLF_ACTIVATE);
}
public static void English()
{
      //load and activate the layout for the current thread
      LoadKeyboardLayout(LANG_EN_US, KLF_ACTIVATE);
}

Top Go to Table of Contents

Building a winForm driver

Add a new C# windows application and place 3 buttons as shown below:

Add reference to the LanguageSelector Dll project, also type: using LanguageSelector;

OK, now we are ready to write the click code for each button:

private void buttonHE_Click(object sender, System.EventArgs e)
{
      KeyboardLayout.Hebrew();
}
private void buttonEN_Click(object sender, System.EventArgs e)
{
      KeyboardLayout.English();
}
private void buttonGetID_Click(object sender, System.EventArgs e){
      string keyboardID;
 keyboardID = KeyboardLayout.getName();
 
 MessageBox.Show(this,"Current keyboard layout ID: " + keyboardID,
  "Keyboard layout",MessageBoxButtons.OK,
  MessageBoxIcon.Information);
}

By clicking the Get Language ID button a message box such that will be shown.

Top Go to Table of Contents

About Yariv Amar

Click here if you want to know more about .

Other articles that may interest you

  • Write a Word Add-In – Part 0
  • Write a Word Add-In – Part I
  • Lengthy Operations on Single Thread in .NET Application
  • Learning Draughts
  • Exceptions and Performance
  • Average Rating :

    Discussion Forums
    Got a programming related question? Hopefully someone has the answer... Want to help out other developers? Visit our discussion forums.

    Sponsored by:

    New Articles

  • Exceptions and Performance
    Almost every time exceptions are mentioned in mailing lists and newsgroups, people say they're really expensive.Let's examine that claim, shall we?

  • Creating multilingual websites - Part 1
    Extend the existing globalization capabilities of .NET to create flexible and powerful multilingual web sites. First, create a custom ResourceManager, and then create custom localized-capable server controls to easily deploy multilingual functionality.

  • Parameter passing in C#
    Many people have become fairly confused about how parameters are passed in C#, particularly with regard to reference types. This page should help to clear up some of that confusion

  • Most Popular Articles

  • LDAP, IIS and WinNT Directory Services
    This article explains how to use .NET Directory Services to retrieve and search directory objects, create new directory objects and edit or delete existing directory objects. Describes Active Directory Application Mode (ADAM) and how to use the IIS, WinNT and LDAP directory (ADSI) provider.

  • An in-depth look at WMI and instrumentation, Part II
    WMI stands for Windows Management Instrumentation and, as the name indicates, is about managing your IT infrastructure this article is the second part of a two-part series.

  • An in-depth look at WMI and instrumentation, Part I
    WMI stands for Windows Management Instrumentation and, as the name indicates, is about managing your IT infrastructure this article provides an in-depth look at WMI and MOM 2005

  • New Books

  • Murach's ASP.NET 2.0 Upgrader's Guide: VB Edition
    What’s new and how to use it! That’s what this book delivers if you’re a VB developer who’s interested in upgrading from ASP.NET 1.x to ASP.NET 2.0.

  • C# in easy steps
    Learn to program with Microsoft’s premier programming language. No previous programming knowledge is assumed. With numerous easy-to-follow examples, this title explains the essentials of object-oriented programming with C#.

  • Murach's ASP.NET web programming with VB.NET
    Murach's ASP.NET web programming with VB.NET by Doug Lowe and Anne Prince is a in depth training and reference book for ASP.NET programming using VB.NET. The book builds upon Murach's previous books and covers more advanced concepts for programming ASP.NET pages.

  • Got Code?

    if you have any article , source code , or anything else you'd like to share with this community that you think others might find useful, please submit it here and we will gladly make it available on this site. submit@developerland.com.
    Partners

    All articles are copyrighted by their individual authors unless otherwise specified , everything else Copyright ©2004-2006 DeveloperLand