Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

Get current keyboard layout


  • Please log in to reply
5 replies to this topic
  • Guests
  • Last active:
  • Joined: --
Hi,

Is there a way to get the currently active keyboard layout?

It maybe tricky, since it can be different for individual applications and it can be changed at any one time by the user.


Is there a function to query this information?

  • Guests
  • Last active:
  • Joined: --
I mean the name of the keyboard layout. If it's set to english or the native keyboard for non-english speakers.

YMP
  • Members
  • 424 posts
  • Last active: Apr 05 2012 01:18 AM
  • Joined: 23 Dec 2006
If you mean the input language of the active window, then here's a way.
F11::
  SetFormat, Integer, H
  WinGet, WinID,, A
  ThreadID:=DllCall("GetWindowThreadProcessId", "UInt", WinID, "UInt", 0)
  InputLocaleID:=DllCall("GetKeyboardLayout", "UInt", ThreadID, "UInt")
  MsgBox, %InputLocaleID%
Return
For US English, the ID will be 0x4090409. For others see Language Codes.

  • Guests
  • Last active:
  • Joined: --
Cool! It really works. Thanks!

Ironhide
  • Members
  • 2 posts
  • Last active: Sep 06 2011 11:15 PM
  • Joined: 06 Sep 2011

If you mean the input language of the active window, then here's a way.

F11::
  SetFormat, Integer, H
  WinGet, WinID,, A
  ThreadID:=DllCall("GetWindowThreadProcessId", "UInt", WinID, "UInt", 0)
  InputLocaleID:=DllCall("GetKeyboardLayout", "UInt", ThreadID, "UInt")
  MsgBox, %InputLocaleID%
Return
For US English, the ID will be 0x4090409. For others see Language Codes.

This method doesn't work in console window. Any solution for console window?

Elfy
  • Members
  • 3 posts
  • Last active: Jun 01 2013 02:06 PM
  • Joined: 27 May 2013

It's an old thread, but I just happen to have a solution (at least for Windows 7) and want to share :)

 

Since win7 it's necessary to apply GetKeyboardLayout to the thread of conhost.exe process associated with console application.

 

The stories of what conhost.exe is and why it's there, etc are plenty on internet. The trick is to reliably find which one is associated with given application. I ended up writing a dll for this, since doing it in ahk would be messy. The method (simplified for readability) is to get a snapshot of all handles in the system with NtQuerySystemInformation and find process handle which is pointing to our console process and owned by one of conhost.exe processes. The conhost process owning it will be the one which is needed. It's second thread will yield relevant information about current keyboard layout.

 

dll sources, binaries and sample script are here: https://github.com/Elfy/getconkbl