Skip to content

Commit 7901360

Browse files
authored
파이썬 가상환경 사용 방법, 배포파일 생성 방법에 사용한 코드
2023/07/28 나이스코딩 유튜브 채널에서 다룬 코드 입니다.
1 parent 4a4c515 commit 7901360

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

keyboard_hook_230728.py

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

keyboard_hook_230728_run.exe

10.9 MB
Binary file not shown.

0 commit comments

Comments
 (0)