6

How is it possible to know the current keyboard layout of a console window having the window handle provided?

For a GUI window it is possible via:

const auto threadId = ::GetWindowThreadProcessId(hwnd, nullptr);
const auto hkl = ::GetKeyboardLayout(idThread);

But this approach doesn't work for console windows.

After some google'ing I ended up with the following results:

  1. Since Win7 the keyboard layout for console application can be received from conhost.exe process, which is a child process of cmd.exe. A little more details can be found here:
    https://autohotkey.com/board/topic/43043-get-current-keyboard-layout/

  2. But in this question:
    c++ how do i get the current console conhost process I found the comment from @RaymondChen that conhost.exe - is an implementation detail, which might not exist in some Windows versions.

So, non-standard workaround seems to be possible. But is there any standard, compatible way to get the right result?

PS, similar question is asked here:
GetKeyboardLayout() doesn't work properly in some cases
But it has no answer on my question.

Alexander Stepaniuk
  • 6,217
  • 4
  • 31
  • 48
  • 2
    This is an issue with Microsoft console which is still open, https://github.com/Microsoft/console/issues/83 – Mayur Oct 05 '18 at 16:32
  • That's another issue there on github. It's about reading locale from _current_ application. But I need to deal with _any_ console session. – Alexander Stepaniuk Oct 06 '18 at 04:15
  • 1
    In Windows 8+, conhost.exe is launched using an IOCTL to the ConDrv device, which executes the host as a child of the caller. This has nothing inherently to do with cmd.exe, which is just a shell. Also, for the console window, the process and thread ID returned by `GetWindowThreadProcessId` is faked based on the console's current effective owner, which is stored as console window data. It's not the thread that really owns the console window, and there is no documented way to query the real thread in the console host process that created the window. – Eryk Sun Oct 06 '18 at 16:39

0 Answers0