본문 바로가기
프로그래밍/Python

[Python/pyautogui] 마우스 움직이기 + 클릭 + 특정 키 조합으로 중단하기

by Mr.noobiest 2023. 12. 6.

Python을 사용하면 마우스나 키보드의 움직임을 원하는 대로 할 수 있고, 이를 응용하면 매크로 프로그램이나 특정 동작을 반복하게 할 수 도 있다,

화면이 안꺼지게 하거나 자동으로 꺼지지 않게 하는등의 간단한 작업에도 사용가능하다.

 

import keyboard
import pyautogui


#강제 종료 키 CTRL+Q
#바로 꺼지는게 아니고 if문에 걸려야하므로 계속 눌러주도록 한다(연타)
stop_key_combination = "ctrl+q"

print("Press {} to stop the loop.".format(stop_key_combination))


#ctrl+q가 안먹힐것을 대비해서 긴급 정지 추가
emergency_count=0
continue_loop = True

while continue_loop:
	emergency_count+=1

    pyautogui.moveTo(300, 300, 2)
    pyautogui.click()
    if keyboard.is_pressed(stop_key_combination):
        print("Stopping the loop.")
        continue_loop = False
    pyautogui.moveTo(100, 300, 2)
    pyautogui.click()
    
    if keyboard.is_pressed(stop_key_combination):
        print("Stopping the loop.")
        continue_loop = False
	elif emergency_count==10:
        print("Stopping the loop.")
        continue_loop = False
728x90
반응형