Click here to Skip to main content
11,631,884 members (69,154 online)
Click here to Skip to main content

Using multiple keyboards with different layouts on the same machine

, 23 Oct 2007 CPOL 49.7K 3.1K 27
Rate this:
Please Sign up or sign in to vote.
RightKeyboard is a program that allows you to transparently switch input languages based on the keyboard you are typing on.

image1.png

Introduction

My laptop has a Portuguese keyboard which I use when I am at home, but at work, I use an external keyboard with a Belgian layout. This is annoying because every time I switch keyboards, I need to go to every open program and manually change its input language with the language bar. Worse, when I start a new program, it starts with the default language, which means that sometimes I have to switch input language also.

One way to work around this problem could be to buy a new keyboard, but I like the IBM model M keyboard and I have yet to find a better keyboard.

Instead of doing that, I wrote a program that switches to the correct input language when I start using a keyboard.

Background

Although more than one keyboard may be connected to the same computer, the Win32 API treats them as if there was only one keyboard. A program receives key strokes from every keyboard, but has no way of knowing which keyboard sent a particular stroke.

Usually, this is exactly what the program wants. It is the role of the Operating System to shield the program from the complexity of dealing with multiple input devices. But in this case, we need to know which device has been used in order to switch the input language. Luckily, the raw input API exposes which device is sending the messages, which solves our problem.

How it works

Describing the raw input API is outside of the scope of this article. The MSDN documentation already provides plenty of information about it. We will stick to the essential. The API allows a program to register to receive notifications each time a keyboard sends an event. When the notification is received, the program checks if it comes from a different device than the previous one. In that case, the program selects the appropriate input language and broadcasts the WM_INPUTLANGCHANGEREQUEST message so that open programs change their input language. Additionally, the default input language is updated using the SystemParametersInfo function, which causes new programs to start with the correct input language.

When a keyboard is used for the first time, the program asks the user which language is to be used. When the program is closed, a file is saved in %USERPROFILE%\Local Settings\Application Data\RightKeyboard that contains the associations between the device identifiers and the input languages. When the program starts, that information is loaded so that the program already knows the correct language for any previously used keyboard.

Using the program

The program is easy to use. Simply run it and an icon will appear in the notification area. The icon allows to close the program when right-clicked. The first time a keyboard is used, a popup will appear with a list of the available input languages. Select the correct language, then click OK. From now on, each time that keyboard is used, every application will switch to that language. More input languages can be added in the Regional and Language Options on the Control Panel.

image2.png

Known Limitations

There are some limitations that more work could probably overcome:

  • Console windows do not switch language when they receive the WM_INPUTLANGCHANGEREQUEST message. Any such window that is already open when the program switches the input language will keep the current language.
  • When the program switches the input language, the currently focused window receives the key before the change. Because of that, the first key that is pressed on another keyboard is always incorrect, unless that particular key is the same on the previous language.
  • After hibernating, the device handles become invalid, which makes the program ask the language for a previously known keyboard.

History

  • 2007/10/24 - First version.

License

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

Share

About the Author

AntoineAubry
Web Developer
Unknown
No Biography provided

You may also be interested in...

Comments and Discussions

 
BugUnhandled exception error on Win7 64 Pin
Member 114344258-Feb-15 17:32
memberMember 114344258-Feb-15 17:32 
GeneralRe: Unhandled exception error on Win7 64 Pin
Gachmuret16-Feb-15 0:56
memberGachmuret16-Feb-15 0:56 
GeneralRe: Unhandled exception error on Win7 64 Pin
Member 1143442522-Feb-15 17:46
memberMember 1143442522-Feb-15 17:46 
GeneralRe: Unhandled exception error on Win7 64 Pin
Gachmuret28-Feb-15 14:14
memberGachmuret28-Feb-15 14:14 
GeneralRe: Unhandled exception error on Win7 64 Pin
Member 114344251-Mar-15 4:27
memberMember 114344251-Mar-15 4:27 
QuestionGreat Util Pin
markiemix21-Nov-14 1:24
membermarkiemix21-Nov-14 1:24 
QuestionVERY GOOD FOR BARCODE SCANNERS Pin
Member 1117456123-Oct-14 1:26
memberMember 1117456123-Oct-14 1:26 
AnswerRe: VERY GOOD FOR BARCODE SCANNERS Pin
Member 309322519-Dec-14 11:39
memberMember 309322519-Dec-14 11:39 
SuggestionSoooo nearly there - can we sponsor fixes? Pin
sxc56785-Sep-14 8:46
membersxc56785-Sep-14 8:46 
QuestionHelp Pin
Chaitanya Kolluru4-Sep-14 0:24
memberChaitanya Kolluru4-Sep-14 0:24 
Question3 bugs found Pin
arenhold30-Jul-14 3:29
memberarenhold30-Jul-14 3:29 
QuestionDvorak Pin
Member 992866720-Mar-13 12:30
memberMember 992866720-Mar-13 12:30 
BugManageable error Pin
GenesisAria27-Dec-12 1:16
memberGenesisAria27-Dec-12 1:16 
GeneralRe: Manageable error Pin
Member 1020232419-Aug-13 6:12
memberMember 1020232419-Aug-13 6:12 
I have the very same problem here.. Frown | :(
any solution about it?
SuggestionRe: Manageable error Pin
mauser7824-Sep-13 11:46
membermauser7824-Sep-13 11:46 
GeneralRe: Manageable error Pin
Gachmuret15-Feb-15 12:01
memberGachmuret15-Feb-15 12:01 
GeneralRe: Manageable error Pin
Member 115925919-Apr-15 22:18
memberMember 115925919-Apr-15 22:18 
GeneralRe: Manageable error Pin
Gachmuret19-Apr-15 10:34
memberGachmuret19-Apr-15 10:34 
QuestionVery Nice! Pin
never-met-me13-Sep-12 8:27
membernever-met-me13-Sep-12 8:27 
GeneralMy vote of 3 Pin
Burak Tunçbilek11-Aug-12 3:49
memberBurak Tunçbilek11-Aug-12 3:49 
QuestionA slight change? Pin
Member 833452930-Oct-11 16:15
memberMember 833452930-Oct-11 16:15 
GeneralAwesome Pin
kjansson26-May-11 4:24
memberkjansson26-May-11 4:24 
GeneralRe: Awesome Pin
brisemec27-May-11 14:01
memberbrisemec27-May-11 14:01 
QuestionKnowing keyboard source Pin
muneersn14-Dec-08 20:05
membermuneersn14-Dec-08 20:05 
AnswerRe: Knowing keyboard source Pin
brisemec15-Dec-08 0:03
memberbrisemec15-Dec-08 0:03 

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 | Terms of Use | Mobile
Web03 | 2.8.150728.1 | Last Updated 23 Oct 2007
Article Copyright 2007 by AntoineAubry
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid