Skip to content
Advertisement

Is there a way to detect movement on “screens” using python? [closed]

For example, if I’m in some game and I want to check whether I’m moving or not. So, for that, is there a way to detect the motion of the player using python? Please Suggest some ideas if possible. Thanks in advance.

Advertisement

Answer

You can do it using pynput

from pynput.mouse import Controller

mouse = Controller()

before = mouse.position

while True:
    current = mouse.position
    if before != current:
        print('Movement detected')
    before = current

The variable before references to the movement that’s been before the current loop, and I basically check if the current position is different than it was the iteration before

You can also check for the keyboard presses

pypi docs

Answering to your comment, it’s also possible, this time using pyautogui

import pyautogui

before = pyautogui.screenshot()

while True:
    current = pyautogui.screenshot()
    if before != current:
        print('Screen is different')
    before = current

pypi docs

Advertisement