【发布时间】:2022-01-03 16:26:35
【问题描述】:
我想在按下特定键或鼠标按钮后运行一个函数。我如何使用 win32 或 ctypes 做到这一点?
【问题讨论】:
标签: python python-3.x
我想在按下特定键或鼠标按钮后运行一个函数。我如何使用 win32 或 ctypes 做到这一点?
【问题讨论】:
标签: python python-3.x
是的,这可以通过 win 低级 API(例如 win32api 和 win32gui)来实现。
但是,通常情况下,尤其是在 python 中,最好使用跨平台包,例如 pyautogui 或 pynput。除非您非常需要替代低级 API。
【讨论】:
这是一个使用低级 win API 进行鼠标监控的示例。
import win32gui
import win32api
class MouseController:
LEFT = 0x01
RIGHT = 0x02
def position():
_, _, x, y = win32gui.GetCursorInfo()
return x, y
def state(button: hex):
if win32api.GetKeyState(button) < 0:
return True
else:
return False
【讨论】: