Search code examples
python-3.xpython-imaging-libraryopencv3.0roi

How to capture mouse movement for the whole window in opencv python?


I know and have seen tons of documentations explaining about how to capture mouse movement within a given window using mouseclick events in opencv. What I want to know is that if is there a way to capture mouse movements (x,y co-ordinates) for the entire screen of my system.

Any link, documentation or code snippet will be really helpful to proceed me with the same.


Solution

  • Depending on your OS, you can do that with pyautogui like this:

    #!/usr/bin/env python3
    
    import time
    import pyautogui
    
    for i in range(10):
        x, y = pyautogui.position()
        print(f'Mouse position: x={x}, y={y}')
        time.sleep(1)