Home > OS >  Is there a win32 function to check for keyboard/mouse input?
Is there a win32 function to check for keyboard/mouse input?

Time:01-05

I want to run a function once a specific key or mouse button is pressed. How may I do this with win32 or ctypes?

CodePudding user response:

Yes, this is possible to do with win low-level APIs such as win32api and win32gui.

However, typically, especially in python, it is better to use a cross-platform package such as pyautogui or pynput. Unless you have a very specfic need for the alternative of a low-level API.

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

CodePudding user response:

Here's an example of mouse monitoring with low-level win APIs.

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
  •  Tags:  
  • Related