python+pyautogui—PC terminal automation (2) keyboard mouse and dialog box operation

Table of contents

mouse operation

Get the coordinates of the mouse position

Get the RGB value of the mouse position

Mouse click

mouse down and release

mouse movement

mouse drag

scroll wheel

keyboard operation

Enter English characters

Common button operation press

push and lift

combination key hotkey

Keep pressing hold

pop-up dialogue box

alert

confirm

prompt

password


mouse operation

Get the coordinates of the mouse position

x, y = pyautogui.position()  # 获取鼠标当前位置坐标
print(x, y)

Get the RGB value of the mouse position

print(pyautogui.screenshot().getpixel((1434, 488)))  # 获取鼠标当前位置坐标的的屏幕RGB颜色值
print(pyautogui.pixel(1434, 488))  # 同.screenshot().getpixel((x, y))

Mouse click

pyautogui.leftClick(1864, 43)  # 单击
pyautogui.rightClick(1864, 43)  # 右击
pyautogui.doubleClick(1864, 43)  # 双击
pyautogui.tripleClick(1864, 43)  # 三次单击
pyautogui.middleClick(1864, 43)  # 中间键点击

pyautogui.click(x=moveToX, y=moveToY, clicks=num_of_clicks, interval=secs_between_clicks, button='left'), the number of clicks clicks; the interval between each click of interval; x, y is empty, and the coordinates of the current position are selected by default

pyautogui.click()  # 鼠标当前位置点击一下
pyautogui.click(1864, 43, 2, 1, button='left')

mouse down and release

# 鼠标按下与释放,模拟拖拽鼠标
pyautogui.mouseDown(1864, 43,  button='left')  # 鼠标左键按下
pyautogui.moveTo(1500, 200)
pyautogui.mouseUp()  # 抬起鼠标按键,添加坐标会拖动鼠标到某个位置后释放

mouse movement

Parameters: duration is the duration

moveTo: move to a certain position, the origin is the upper left corner of the computer screen (relative to the real screen),

for i in range(1, 5):
    print(i * 100, i * 100)
    pyautogui.moveTo(i * 100, i * 100, duration=.2)  # 移动到某个位置,原点为电脑屏幕左上角(相对真个屏幕而言)

moveRel (move): Move from the current position to the origin (the origin is the current position), and move to a certain offset vector (relative to the current position), which can control the movement of up, down, left, and right

for i in range(1, 5):
    print(i * 100, i * 100)
    pyautogui.moveRel(i * 100, i * 100, duration=.2)  # 从当前位置移动为原点(原点为当前位置),移动到某个偏移向量(相对当前位置而言),可控制上下左右的移动
    
pyautogui.move(500, 500, .5)    # 同moveRel

Ease/Fade functions change the speed and direction of cursor movement. Usually the mouse moves in a straight line at a constant speed, which is the linear easing/gradient function. PyAutoGUI has 30 kinds of easing/gradient functions, which can be viewed through pyautogui.ease...

pyautogui.moveTo(100, 100, 2, pyautogui.easeInQuad)  # 由慢到快,不断加速
pyautogui.moveTo(100, 100, 2, pyautogui.easeOutQuad)  # 又快到慢,不断减速
pyautogui.moveTo(100, 100, 2, pyautogui.easeInOutQuad)  # 开始和结束都快,中间比较慢
pyautogui.moveTo(100, 100, 2, pyautogui.easeInBounce)  # 一步一徘徊前进
pyautogui.moveTo(100, 100, 2, pyautogui.easeInElastic)  # 徘徊幅度更大,甚至超过起点和终点

mouse drag

pyautogui.dragTo(1867, 125,duration=.2, button='left')  # 按住鼠标左键,将鼠标拖拽到某个位置(相对于屏幕而言)
pyautogui.dragRel(0, 100, duration=0.5)  # 按住鼠标左键,将鼠标拖拽到某个位置(相对当前位置而言)
pyautogui.drag(-300, 100, .5, button='left')  # 同dragRel

scroll wheel

# pyautogui.hscroll(500)   # 滚轮水平滚动
# pyautogui.vscroll(500)  # 滚轮垂直滚动
# pyautogui.scroll(clicks=amount_to_scroll, x=moveToX, y=moveToY)  # amount_to_scroll参数表示滚动的格数,正数则页面向上滚动,负数则向下滚动
for _ in range(10):
    time.sleep(.2)
    pyautogui.scroll(100)

keyboard operation

Keyboard operation, the key names that can be input by the press(), keyDown(), keyUp() and hotkey() functions in the pyautogui.KEYBOARD_KEYS array:

pyautogui.KEYBOARD_KEYS = ['\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']

Enter English characters

pyautogui.typewrite('test', interval=.25)  # 输入字符串,interval参数为每次键盘输入的间隔时间,默认为0
pyautogui.write('test', interval=.25)  # 英文输入,同上

Common button operation press

pyautogui.press('enter')  # 点击回车键
pyautogui.press(['left', 'down', 'right', 'up'])  # 连续按键

push and lift

pyautogui.keyDown('win')  # 按下win键
pyautogui.press('r')
pyautogui.keyUp('win')  # 松开win键

combination key hotkey

pyautogui.hotkey('win', 'e')

Keep pressing hold

with pyautogui.hold('win'):
# 先按住win,再按三次其他按键,再释放win
    for i in ['e', 'i', 'r']:
        time.sleep(2)
        pyautogui.press(i)

pop-up dialogue box

alert

A simple message popup with text and an OK button, the user clicks to return to the text of the button

a = pyautogui.alert(text='确认要删除此文件吗?', title='删除文件', button='确定')
print(a)

  

confirm

Display a simple message pop-up window with text, OK and Cancel buttons. After the user clicks, the text of the clicked button is returned, and a list of custom numbers and texts is supported.

# b = pyautogui.confirm(text='请选择一个数字', title='', buttons=range(10))
# print(b)
pyautogui.confirm(text='是否进行下一步?', title='', buttons=['下一步', '退出'])

  

prompt

A message pop-up window that can be input, with OK and Cancel buttons, the user clicks the OK button to return the entered text, and clicks the Cancel button to return None

c = pyautogui.prompt(text='输入要提交的内容:', title='', default='')
print(c)

  

password

The style is the same as prompt(), which is used to input password, and the message is represented by *. With OK and Cancel buttons. The user clicks the OK button to return the entered text, and clicks the Cancel button to return None

pyautogui.password(text='请输入密码:', title='', default='', mask='*')

Guess you like

Origin blog.csdn.net/JBY2020/article/details/128080730