Created
July 7, 2019 13:59
-
-
Save Wegazz/05bfd2147a7ea5dc804baf13d12353d6 to your computer and use it in GitHub Desktop.
Gets the keyboard language in use by the current active window process.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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