我安装了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)
答案 0 :(得分:1)
检查this answer类似的问题;您似乎需要使用GetGUIThreadInfo来确定桌面上当前的活动线程,然后将其传递给GetKeyboardLayout。