- Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathkeyboard_hook.py
More file actions
Latest commit
49 lines (39 loc) · 1.89 KB
/
Copy pathkeyboard_hook.py
File metadata and controls
49 lines (39 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
importkeyboard
importthreading
importpyautogui
fromtimeimportsleep
classHook(threading.Thread):
def__init__(self):
super(Hook, self).__init__() # 부모 클래스 __init__ 실행
self.daemon=True# 데몬 쓰레드로 설정
self.event=False# f4가 눌리면 event 발생
self.my_xy= [] # 좌표 저장 리스트
keyboard.unhook_all() # 후킹 초기화
keyboard.add_hotkey('f4', print, args=['\n종료합니다']) # f4가 눌리면 print 실행
keyboard.add_hotkey('f2', print, args=['\n좌표값이 추가되었습니다']) # f2가 눌리면 print 실행
defrun(self): # run 메소드 재정의
whileTrue:
key=keyboard.read_hotkey(suppress=False) # hotkey를 계속 읽음
ifkey=='f4': # f4 받은 경우
self.event=True# event 클래스 변수를 True로 설정
# print("\n", self.my_xy)
withopen(r"config.txt", "w") asf:
foriinself.my_xy:
f.write("{},{}\n".format(i[0], i[1]))
break# 반복문 탈출
elifkey=='f2':
position=pyautogui.position()
self.my_xy.append((position.x, position.y))
deftrack_pos():
h=Hook() # 훅 쓰레드 생성
h.start() # 쓰레드 실행
print('size:', pyautogui.size()) # 화면 크기 출력
whileTrue: # 무한루프
ifh.event==True: # h.event가 True이면(f4 입력받은 경우) 종료
break
position=pyautogui.position() # 마우스 현재 위치(x, y) 반환
print(f'\r{position.x:4}, {position.y:4}', end='')
sleep(0.05)
h.join() # 쓰레드 종료까지 대기
keyboard.unhook_all() # 후킹 해제
track_pos()