如何通过ctypes python设置用户的输入语言?

时间:2018-01-16 17:45:30

标签: python winapi ctypes

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

在本主题中有明确的答案如何获取当前的用户语言,但我想使用set with get。

这可能应该是一个功能

ctypes.windll.user32.ActivateKeyboardLayout()

有一些参数。

在python中有没有办法如何使用这个函数(就像上面主题中描述的那样?)

1 个答案:

答案 0 :(得分:0)

我刚刚阅读了问题更新(已恢复)。以下是通过[Python 3]: ctypes - A foreign function library for Python

进行操作的方法

code.py

#!/usr/bin/env python3

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("{} returned: {}".format(LoadKeyboardLayout.__name__, hex(klh)))
    print("{} returned: {}".format(GetLastError.__name__, GetLastError()))


if  __name__ == "__main__":
    main()

<强>输出

(py35x64_test) E:\Work\Dev\StackOverflow\q048287040>"e:\Work\Dev\VEnvs\py35x64_test\Scripts\python.exe" code.py
LoadKeyboardLayoutA returned: 0x4090409
GetLastError returned: 0