Click here to Skip to main content
15,885,720 members
Articles / Programming Languages / C#

Using multiple keyboards with different layouts on the same machine

Rate me:
Please Sign up or sign in to vote.
4.83/5 (12 votes)
23 Oct 2007CPOL3 min read 171.2K   7.3K   30  
RightKeyboard is a program that allows you to transparently switch input languages based on the keyboard you are typing on.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using RightKeyboard.Win32;

namespace RightKeyboard {
	public partial class LayoutSelectionDialog : Form {
		public LayoutSelectionDialog() {
			InitializeComponent();

			LoadLanguageList();
		}

		private void LoadLanguageList() {
			lbLayouts.Items.Clear();
			recentLayoutsCount = 0;

			IntPtr[] installedLayouts = API.GetKeyboardLayoutList();

			foreach(Layout layout in RightKeyboard.Layout.GetLayouts()) {
				foreach(IntPtr installedLayout in installedLayouts) {
					ushort languageId = unchecked((ushort)installedLayout.ToInt32());
					if(layout.Identifier == languageId) {
						lbLayouts.Items.Add(layout);
					}
				}
			}

			lbLayouts.SelectedIndex = 0;
		}

		private int recentLayoutsCount = 0;
		private Layout selectedLayout;
		private bool okPressed = false;

		public new Layout Layout {
			get {
				return selectedLayout;
			}
		}

		private void btOk_Click(object sender, EventArgs e) {
			selectedLayout = (Layout)lbLayouts.SelectedItem;
			okPressed = true;
			Close();
		}

		private void lbLayouts_SelectedIndexChanged(object sender, EventArgs e) {
			btOk.Enabled = lbLayouts.SelectedIndex != recentLayoutsCount || recentLayoutsCount == 0;
		}

		private void lbLayouts_DoubleClick(object sender, EventArgs e) {
			if(btOk.Enabled) {
				btOk_Click(this, EventArgs.Empty);
			}
		}

		protected override void OnClosing(CancelEventArgs e) {
			e.Cancel = !okPressed;
			okPressed = false;
			base.OnClosing(e);
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Unknown
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions