1+ import keyboard
2+ import threading
3+ import pyautogui
4+ from time import sleep
5+
6+
7+ class Hook (threading .Thread ):
8+ def __init__ (self ):
9+ super (Hook , self ).__init__ () # 부모 클래스 __init__ 실행
10+ self .daemon = True # 데몬 쓰레드로 설정
11+ self .event = False # f4가 눌리면 event 발생
12+ self .my_xy = [] # 좌표 저장 리스트
13+ keyboard .unhook_all () # 후킹 초기화
14+ keyboard .add_hotkey ('f4' , print , args = ['\n 종료합니다' ]) # f4가 눌리면 print 실행
15+ keyboard .add_hotkey ('f2' , print , args = ['\n 좌표값이 추가되었습니다' ]) # f2가 눌리면 print 실행
16+
17+ def run (self ): # run 메소드 재정의
18+ while True :
19+ key = keyboard .read_hotkey (suppress = False ) # hotkey를 계속 읽음
20+ if key == 'f4' : # f4 받은 경우
21+ self .event = True # event 클래스 변수를 True로 설정
22+ # print("\n", self.my_xy)
23+
24+ with open (r"config.txt" , "w" ) as f :
25+ for i in self .my_xy :
26+ f .write ("{},{}\n " .format (i [0 ], i [1 ]))
27+
28+ break # 반복문 탈출
29+
30+ elif key == 'f2' :
31+ position = pyautogui .position ()
32+ self .my_xy .append ((position .x , position .y ))
33+
34+
35+ def track_pos ():
36+ h = Hook () # 훅 쓰레드 생성
37+ h .start () # 쓰레드 실행
38+ print ('size:' , pyautogui .size ()) # 화면 크기 출력
39+
40+ while True : # 무한루프
41+ if h .event == True : # h.event가 True이면(f4 입력받은 경우) 종료
42+ break
43+ position = pyautogui .position () # 마우스 현재 위치(x, y) 반환
44+ print (f'\r { position .x :4} , { position .y :4} ' , end = '' )
45+ sleep (0.05 )
46+ h .join () # 쓰레드 종료까지 대기
47+ keyboard .unhook_all () # 후킹 해제
48+
49+ track_pos ()
0 commit comments