Skip to content

Instantly share code, notes, and snippets.

@Wegazz
Created July 7, 2019 13:59
Show Gist options
  • Save Wegazz/05bfd2147a7ea5dc804baf13d12353d6 to your computer and use it in GitHub Desktop.
Save Wegazz/05bfd2147a7ea5dc804baf13d12353d6 to your computer and use it in GitHub Desktop.
Gets the keyboard language in use by the current active window process.
import ctypes # Required to obtain the keyboard layout
def get_keyboard_language():
"""
Gets the keyboard language in use by the current
active window process.
"""
# My keyboard is set to the English - United States keyboard
# For debugging Windows error codes in the current thread
user32 = ctypes.WinDLL('user32', use_last_error=True)
curr_window = user32.GetForegroundWindow()
thread_id = user32.GetWindowThreadProcessId(curr_window, 0)
# Made up of 0xAAABBBB, AAA = HKL (handle object) & BBBB = language ID
klid = user32.GetKeyboardLayout(thread_id)
# print(klid)
#67699721
# Language ID -> low 10 bits, Sub-language ID -> high 6 bits
# Extract language ID from KLID
lid = klid & (2**16 - 1)
# Convert language ID from decimal to hexadecimal
lid_hex = hex(lid)
print(lid_hex)
#'0x409' - en
#'0x419' - ru
get_keyboard_language()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment