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

☆樱花仙子☆ 提交于 2019-12-23 01:41:28

问题


I installed pyHook and successfully attached handlers to keyboard events, but now I need to find out whether the user is typing in English layout or other layouts. I couldn't find this information in the event objects.

How do I find on windows what the typing language in the focused window is? I tried playing with GetKeyboardLayout with no success (it always return the same value whether I type in English or in a different language - in my case Hebrew).

Thanks

Solved thanks to BrendanMcK's reference.

Python code:

from ctypes import windll, c_ulong, byref, sizeof, Structure
user32 = windll.user32

class RECT(Structure):
    _fields_ = [
        ("left", c_ulong),
        ("top", c_ulong),
        ("right", c_ulong),
        ("bottom", c_ulong)];

class GUITHREADINFO(Structure):
    _fields_ = [
    ("cbSize", c_ulong),
    ("flags", c_ulong),
    ("hwndActive", c_ulong),
    ("hwndFocus", c_ulong),
    ("hwndCapture", c_ulong),
    ("hwndMenuOwner", c_ulong),
    ("hwndMoveSize", c_ulong),
    ("hwndCaret", c_ulong),
    ("rcCaret", RECT)
    ]

def get_layout():
    guiThreadInfo = GUITHREADINFO(cbSize=sizeof(GUITHREADINFO))
    user32.GetGUIThreadInfo(0, byref(guiThreadInfo))
    dwThread = user32.GetWindowThreadProcessId(guiThreadInfo.hwndCaret, 0)
    return user32.GetKeyboardLayout(dwThread)

回答1:


Check this answer to a similar question; seems you need to use GetGUIThreadInfo to determine the current active thread on the desktop, and then pass that to GetKeyboardLayout.



来源:https://stackoverflow.com/questions/14197782/win32api-pyhook-how-to-get-the-users-typing-language

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!