我已经查看了 winapi 模块和其他内容,但我不知道如何检测它。目前,我正在使用按键来检测菜单何时打开和关闭,但这并不可靠,因为当您单击应用程序启动它时,Windows 菜单会关闭。如果有人能告诉我如何以不同的方式检测它,我将不胜感激。
我尝试使用按键库来检测它,但它不可靠,因为它无法检测到我想要它做的一半内容。我希望它能够正确检测 Windows 菜单何时打开以及何时关闭。
def getForegroundWindowTitle() -> Optional[str]:
hWnd = windll.user32.GetForegroundWindow()
length = windll.user32.GetWindowTextLengthW(hWnd)
buf = create_unicode_buffer(length + 1)
windll.user32.GetWindowTextW(hWnd, buf, length + 1)
# 1-liner alternative: return buf.value if buf.value else None
if buf.value:
return buf.value
else:
return None
if getForegroundWindowTitle() == "Search":
#gets the window in focus, fortunately windows focuses the start menu whenever the key is pressed, the window name is called search
#do something here
pass