【问题标题】:Is there a win32 function to check for keyboard/mouse input?是否有用于检查键盘/鼠标输入的 win32 功能?
【发布时间】:2022-01-03 16:26:35
【问题描述】:

我想在按下特定键或鼠标按钮后运行一个函数。我如何使用 win32 或 ctypes 做到这一点?

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    是的,这可以通过 win 低级 API(例如 win32apiwin32gui)来实现。

    但是,通常情况下,尤其是在 python 中,最好使用跨平台包,例如 pyautoguipynput。除非您非常需要替代低级 API。

    https://pyautogui.readthedocs.io/en/latest/mouse.html

    【讨论】:

    • 我希望使用较低级别的 API 作为 pynput,而其他库没有我想要的全部功能。
    【解决方案2】:

    这是一个使用低级 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-13
      • 2011-06-01
      • 1970-01-01
      • 2020-12-28
      • 1970-01-01
      • 2011-12-09
      • 2018-05-29
      相关资源
      最近更新 更多