Computer Language/Python
파이썬(Python) 매크로 만들기 ( 마우스 , 키보드 제어)
국산곰탱이
2021. 3. 12. 12:18
반응형
마우스 키보드 제어를 위해 라이브러리 설치
pip install mouse
pip install keyboard
1) 마우스 위치 좌표 출력
- 소스코드)
1
2
3
4
5
6
7
8
9
|
import mouse as mo
import time
def mo_posi():
while True:
print(mo.get_position())
time.sleep(1)
if __name__ == "__main__":
mo_posi()
|
cs |
- 실행결과)

실행 후 자동으로 1초당 마우스 좌표를 출력해 준다.
2) 키보드로 좌표 시작 중지 설정
- 소스코드)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
import mouse as mo
import keyboard as key
import time
def mo_posi():
loop =False
while True:
if key.is_pressed('F2') == True:
loop = True
if key.is_pressed('F3') == True:
loop = False
if loop == True:
print(mo.get_position())
time.sleep(0.5)
if __name__ == "__main__":
mo_posi()
|
cs |
실행 결과는 그림 1)과 똑같으며 F2 누를 시 마우스 좌표를 0.5초마다 출력 F3 누르면 정지
3) 좌표 원하는 회수 입력
-)소스코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
import mouse as mo
import keyboard as key
def locSave(cnt):
print('mouse move and press f2')
returnList = []
wcnt = 0
state = False
while True:
val = key.is_pressed('F2')
if state != val:
if val == True:
wcnt += 1
returnList.append(mo.get_position())
print('catch')
state = val
if wcnt >= cnt:
return returnList
if __name__ == "__main__":
count = int(input("Count :"))
print(locSave(count))
|
cs |
-) 실행결과
처음 희망하는 좌표 개수를 입력하고 F2키를 통해 마우스를 원하는 위치에서 눌러 입력
반응형