1

win32api & pyhook - How to get the user's typing language?

In this topic there is clear answer how to get current user language, but I wanna use set with get.

This probably should be a function

ctypes.windll.user32.ActivateKeyboardLayout()

With some params.

Is there a way in python how to use this function (in the way like it was described in topic above?)

Blue
  • 22,608
  • 7
  • 62
  • 92
Vova
  • 563
  • 8
  • 20
  • You want to do it for the current thread? Or how? You'll have to be more specific. Also you should provide your attempts to get this done, instead of waiting for someone to magically do it for you. – CristiFati Jan 31 '18 at 21:02
  • yes, in same thread, and i was already did it myself via module win32api using "win32api.LoadKeyboardLayout(laycode,1)", where laycode is '00000409' for english and can be found here https://msdn.microsoft.com/en-us/library/windows/desktop/dd318693(v=vs.85).aspx . I found decision i looked for, but i still want to know if it possible via ctypes – Vova Jan 31 '18 at 22:35
  • I see. However could you post (in the question not in a comment) the *win32api* code so it's obvious what it *ctypes* counterpart should do? – CristiFati Feb 01 '18 at 07:46
  • [You do not need to mark questions as "SOLVED" via editing the title](//meta.stackexchange.com/a/116105/295637), or [posting updates/thanks in posts](//meta.stackexchange.com/a/109959/295637). Simply add your own answer, and mark as accepted. Anything additional can be perceived as noise for future visitors. See: [Can I answer my own question?](//stackoverflow.com/help/self-answer). – Blue Feb 01 '18 at 17:04

1 Answers1

0

I just read the question update (that was reverted). Here's how to do it via [Python 3.Docs]: ctypes - A foreign function library for Python:

code00.py:

#!/usr/bin/env python3

import sys
import ctypes
from ctypes import wintypes


KLF_ACTIVATE = 0x00000001


def main():
    locale_id_bytes = b"00000409"
    klid = ctypes.create_string_buffer(locale_id_bytes)
    user32_dll = ctypes.WinDLL("user32")
    kernel32_dll = ctypes.WinDLL("kernel32")
    LoadKeyboardLayout = user32_dll.LoadKeyboardLayoutA
    LoadKeyboardLayout.argtypes = [wintypes.LPCSTR, wintypes.UINT]
    LoadKeyboardLayout.restype = wintypes.HKL
    GetLastError = kernel32_dll.GetLastError
    GetLastError.restype = wintypes.DWORD

    klh = LoadKeyboardLayout(klid, KLF_ACTIVATE)
    print("{0:s} returned: 0x{1:016X}".format(LoadKeyboardLayout.__name__, klh))
    print("{0:s} returned: {1:d}".format(GetLastError.__name__, GetLastError()))


if  __name__ == "__main__":
    print("Python {0:s} {1:d}bit on {2:s}\n".format(" ".join(item.strip() for item in sys.version.split("\n")), 64 if sys.maxsize > 0x100000000 else 32, sys.platform))
    main()
    print("\nDone.")

Output:

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q048287040]> "e:\Work\Dev\VEnvs\py_064_03.07.03_test0\Scripts\python.exe" code00.py
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] 64bit on win32

LoadKeyboardLayoutA returned: 0x0000000004090409
GetLastError returned: 0

Done.

More (topic related) details on [MS.Docs]: LoadKeyboardLayoutA function.

CristiFati
  • 38,250
  • 9
  • 50
  • 87
  • And what this code do? From where you got those hex values and byte strings? – banderlog013 Oct 18 '19 at 12:58
  • @banderlog013: The code does what the question is about: loading an input locale. All the constants and their meaning can be found in the *URL* that I've just added at the end. – CristiFati Oct 18 '19 at 13:22