I need to get the Current Windows Keyboard Layout for my WPF application to map each key correctly and handle AZERTY as well as QWERTY and QWERTZ (and so on...)
I noticed a problem since I am working with a French layout (azerty) but my windows is displayed in English.
I tried various methods to get the layout correctly but without results :
var test1 = InputLanguageManager.Current.CurrentInputLanguage;
and
var test2 = CultureInfo.CurrentCulture;
I tried by having ENG language with AZERTY layout, ENG language with QWERTY layout and FRA language with AZERTY layout but the output from my tests were always different. I could get the language displayed correctly (en-GB) but not the layout.
The following PowerShell1 script declares
Get-KeyboardLayoutForPid
function which reliably gets the Current Windows Keyboard Layout for any process2.The
Get-KeyboardLayoutForPid
function contains a Comment-Based Help placed at the end of its body.I hope that its principle is implementable in
C#
easy.The main idea of my approach:
CurrentInputLanguage
) for a given process is the (user-dependant) default one (DefaultInputLanguage
).allLayoutsRaw
). Note this trick for a console application: collect keyboard layouts associated with every thread of childconhost
process as well.DefaultInputLanguage
in theallLayoutsRaw
collection then it's the sought-after one (retLayouts
).1 Does not work in PowerShell Core (
pwsh.exe
).2 Might fail for
explorer
process in a language multifarious environment, see an example of failing scenario in the Comment-Based help.I am not sure of the ask - whether you want to know the current keyboard layout or you want to set the keyboard layout.
In both cases, InputLanguageManager should help.
You can try setting input language manager to appropriate cultureInfo object. This should change the keyboard layout for your WPF application