Click here to Skip to main content
15,879,184 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 170.9K   7.3K   30   55
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)


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

 
BugException produced when switching to arabic layout Pin
qwerty51420-Jan-21 23:57
qwerty51420-Jan-21 23:57 
QuestionHow to reassing languages? Pin
Member 1500847930-Nov-20 23:12
Member 1500847930-Nov-20 23:12 
QuestionMultiple keyboards for language Pin
CCanadaK29-Mar-20 20:41
CCanadaK29-Mar-20 20:41 
AnswerRe: Multiple keyboards for language Pin
Member 852350227-May-20 13:49
Member 852350227-May-20 13:49 
GeneralRe: Multiple keyboards for language Pin
CCanadaK27-May-20 23:52
CCanadaK27-May-20 23:52 
GeneralRe: Multiple keyboards for language Pin
CCanadaK6-Jul-20 20:59
CCanadaK6-Jul-20 20:59 
GeneralRe: Multiple keyboards for language Pin
User 1404607828-Jan-21 5:47
User 1404607828-Jan-21 5:47 
QuestionAutoStart before login Pin
Member 1473676621-Mar-20 2:15
Member 1473676621-Mar-20 2:15 
QuestionProfessional use ? Copyright ? Pin
Member 1474126011-Feb-20 1:25
Member 1474126011-Feb-20 1:25 
Is there any problem or rights to be respected using it professionally in the company I work for?
Please advise if so. We would want to use it for BarcodeScanning parallel with a common German keyboard layout ...
best regards,
Don Smile | :)
QuestionSwitching Keyboard Layout with Barcode Scanner Pin
Member 1474126011-Feb-20 1:19
Member 1474126011-Feb-20 1:19 
QuestionWhy it needs time to change the layout? Pin
Member 147367665-Feb-20 23:30
Member 147367665-Feb-20 23:30 
AnswerRe: Why it needs time to change the layout? Pin
Member 147367666-Feb-20 0:09
Member 147367666-Feb-20 0:09 
QuestionQWERTY vs DVORAK Pin
Djaqed24-Jul-19 15:58
Djaqed24-Jul-19 15:58 
AnswerRe: QWERTY vs DVORAK Pin
Member 852350227-May-20 19:59
Member 852350227-May-20 19:59 
BugThis is not compatible with the Windows 10 Kernel Pin
Member 132891842-Jul-17 8:32
Member 132891842-Jul-17 8:32 
QuestionNot working Windows 7 / US QWERTY / BE AZERTY Pin
Member 1281869626-Oct-16 23:10
Member 1281869626-Oct-16 23:10 
AnswerRe: Not working Windows 7 / US QWERTY / BE AZERTY Pin
Member 852350227-May-20 19:59
Member 852350227-May-20 19:59 
QuestionIt does not work on Windows 10 Pin
Luis Enrique Rodriguez25-Oct-16 0:12
Luis Enrique Rodriguez25-Oct-16 0:12 
Questionawesome works on win10 Pin
Member 1273243911-Sep-16 5:15
Member 1273243911-Sep-16 5:15 
SuggestionWindows 7 unhandled exception Pin
Gábor Angyal16-Oct-15 2:59
professionalGábor Angyal16-Oct-15 2:59 
GeneralRe: Windows 7 unhandled exception Pin
bugmenot2222422-Oct-15 4:52
professionalbugmenot2222422-Oct-15 4:52 
GeneralRe: Thx for bugfix release! Pin
Gachmuret22-Oct-15 13:15
Gachmuret22-Oct-15 13:15 
GeneralA little gem - should have googled this years ago... Pin
Member 119818072-Oct-15 0:39
Member 119818072-Oct-15 0:39 
BugUnhandled exception error on Win7 64 Pin
Member 114344258-Feb-15 17:32
Member 114344258-Feb-15 17:32 
GeneralRe: Unhandled exception error on Win7 64 Pin
Gachmuret16-Feb-15 0:56
Gachmuret16-Feb-15 0:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.