본문 바로가기
Programming/Python

마우스 클릭 시 좌표 알아내기 - 파이썬 (Python) 코딩

by 심코딩 2022. 5. 21.

오늘은 기본 문법은 아니고 그냥 다른 일을 진행하다가 알게 된 모듈에 대해 적어 보려한다.

 

원래 캡처프로그램을 만들려고 알아보다가 알게 된 것인데, 마우스 클릭 시 클릭 했을 때의 좌표를 저장하는 것이다.

바로 pynput이라는 라이브러리를 통해 마우스를 제어하는 것인데, 해당 관련 자세한 사항은 아래 웹사이트에서 확인 가능하다.

 

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

 

Handling the mouse — pynput 1.7.6 documentation

Parameters: on_move (callable) – The callback to call when mouse move events occur. It will be called with the arguments (x, y), which is the new pointer position. If this callback raises StopException or returns False, the listener is stopped. on_click

pynput.readthedocs.io

마우스 클릭 시 좌표를 알아내는 코드는 아래와 같다.

 

from pynput.mouse import Listener

coord = []

def click(x,y, button, pressed):
        if pressed:
            x = int(x)
            y = int(y)
            coord.append(x)
            coord.append(y)
            print(coord)
        
with Listener(on_click = click) as Listener:
    Listener.join()

 

댓글