【问题标题】:win32api & pyhook - How to get the user's typing language?win32api & pyhook - 如何获取用户的输入语言?
【发布时间】:2013-01-07 14:19:46
【问题描述】:

我安装了pyHook 并成功地将处理程序附加到键盘事件,但现在我需要确定用户是在输入英文布局还是其他布局。我在事件对象中找不到此信息。

如何在 Windows 上找到焦点窗口中的输入语言?我尝试使用 GetKeyboardLayout 没有成功(无论我用英语还是其他语言输入,它总是返回相同的值 - 在我的例子中是希伯来语)。

谢谢

感谢 BrendanMcK 的参考解决。

Python 代码:

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)

【问题讨论】:

  • 不要在您的问题中发布答案。而是提交答案。这允许其他人对其进行投票,而您可以接受它。结果是更好的用户体验,让未来的访问者更容易找到好的答案。

标签: python winapi multilingual keyboard-events pyhook


【解决方案1】:

检查this answer 到类似的问题;看来您需要使用 GetGUIThreadInfo 来确定桌面上当前的活动线程,然后将其传递给 GetKeyboardLayout。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-02
    • 2018-01-31
    • 1970-01-01
    • 1970-01-01
    • 2016-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多