我安装了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)