Python 에서는 다양한 편리한 라이브러리를 제공하니다.

화면을 제어하기 위해서 가장 기본이 되는 기능이 마우스, 키보드 제어와 관련된 것인데요.

해당 기능을 쉽게 구현하기 위한 라이브러리와 예제를 찾아보려고 합니다.

 

라이브러리 이름 : pyautogui

라이브러리 설치 방법 : 콘솔에서 pip install pyautogui 명령어를 입력하여 설치

 

 

주요 마우스 관련 코드 정리

import pyautogui

# 좌표 객체 얻기 
position = pyautogui.position()

# 화면 전체 크기 확인하기
print(pyautogui.size())

# x, y 좌표
print(position.x)
print(position.y)

# 마우스 이동 (x 좌표, y 좌표)
pyautogui.moveTo(500, 500)

# 마우스 이동 (x 좌표, y 좌표 2초간)
pyautogui.moveTo(100, 100, 2)  

# 마우스 이동 ( 현재위치에서 )
pyautogui.moveRel(200, 300, 2)

# 마우스 클릭
pyautogui.click()

# 2초 간격으로 2번 클릭
pyautogui.click(clicks= 2, interval=2)

# 더블 클릭
pyautogui.doubleClick()

# 오른쪽 클릭
pyautogui.click(button='right')

# 스크롤하기 
pyautogui.scroll(10)

# 드래그하기
pyautogui.drag(0, 300, 1, button='left')

 

 

주요 키보드 관련 코드 정리

import pyautogui 
import pyperclip

pyautogui.write('hello world!') # 괄호 안의 문자를 타이핑 합니다. 한글은 인식하지 않습니다.
pyautogui.write('hello world!', interval=0.25) # 각 문자를 0.25마다 타이핑합니다. 

pyperclip.copy("안녕하세요") # 클립보드에 텍스트를 복사합니다. 
# 한글을 입력하려면, pyperclip 모듈을 통해서 한글을 복사 후 입력할 수 있습니다.
pyautogui.hotkey('ctrl', 'v') # 붙여넣기 (hotkey 설명은 아래에 있습니다.)

pyautogui.press('shift') # shift 키를 누릅니다.
pyautogui.press('ctrl') # ctrl 키를 누릅니다. 

# keyDown()은 키를 누른채로 있는거고 keyUp()은 누른 키를 떼는 겁니다.
pyautogui.keyDown('ctrl') # ctrl 키를 누른 상태를 유지합니다.
pyautogui.press('c') # c key를 입력합니다. 
pyautogui.keyUp('ctrl') # ctrl 키를 뗍니다. 

# 키를 여러번 입력 하려면 다음과 같은 방식으로 입력하면 됩니다.
pyautogui.press(['left', 'left', 'left']) # 왼쪽 방향키를 세번 입력합니다.
pyautogui.press('left', presses=3) # 왼쪽 방향키를 세번 입력합니다. 
pyautogui.press('enter', presses=3, interval=3) # enter 키를 3초에 한번씩 세번 입력합니다. 

# 여러 키를 동시에 입력해야 할 때 활용합니다.
pyautogui.hotkey('ctrl', 'c') # ctrl + c 키를 입력합니다. 

# 키보드 키의 명칭 리스트
['\t', '\n', '\r', ' ', '!', '"', '#', '$', '%', '&', "'", '(',
')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', ':', ';', '<', '=', '>', '?', '@', '[', '\\', ']', '^', '_', '`',
'a', 'b', 'c', 'd', 'e','f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~',
'accept', 'add', 'alt', 'altleft', 'altright', 'apps', 'backspace',
'browserback', 'browserfavorites', 'browserforward', 'browserhome',
'browserrefresh', 'browsersearch', 'browserstop', 'capslock', 'clear',
'convert', 'ctrl', 'ctrlleft', 'ctrlright', 'decimal', 'del', 'delete',
'divide', 'down', 'end', 'enter', 'esc', 'escape', 'execute', 'f1', 'f10',
'f11', 'f12', 'f13', 'f14', 'f15', 'f16', 'f17', 'f18', 'f19', 'f2', 'f20',
'f21', 'f22', 'f23', 'f24', 'f3', 'f4', 'f5', 'f6', 'f7', 'f8', 'f9',
'final', 'fn', 'hanguel', 'hangul', 'hanja', 'help', 'home', 'insert', 'junja',
'kana', 'kanji', 'launchapp1', 'launchapp2', 'launchmail',
'launchmediaselect', 'left', 'modechange', 'multiply', 'nexttrack',
'nonconvert', 'num0', 'num1', 'num2', 'num3', 'num4', 'num5', 'num6',
'num7', 'num8', 'num9', 'numlock', 'pagedown', 'pageup', 'pause', 'pgdn',
'pgup', 'playpause', 'prevtrack', 'print', 'printscreen', 'prntscrn',
'prtsc', 'prtscr', 'return', 'right', 'scrolllock', 'select', 'separator',
'shift', 'shiftleft', 'shiftright', 'sleep', 'space', 'stop', 'subtract', 'tab',
'up', 'volumedown', 'volumemute', 'volumeup', 'win', 'winleft', 'winright', 'yen',
'command', 'option', 'optionleft', 'optionright']

 

주요 이미지 관련 코드 정리

import pyautogui as pg

# 원하는 이미지를 5.png로 저장해 줍니다. 그리고 이미지파일을 소스파일과 같은 위치로 이동시킵니다.
button5location = pg.locateOnScreen('5.png') # 이미지가 있는 위치를 가져옵니다. 
print(button5location)

# Box(left=1295, top=540, width=38, height=32)
# left = x 좌표, top = y 좌표, width = 너비, height = 높이 입니다.

button5location = pg.locateOnScreen('5.png')
point = pg.center(button5location) # Box 객체의 중앙 좌표를 리턴합니다. 
print(point)

# 출력결과는 Point 객체 입니다. Point(x=1314, y=556)

+ Recent posts