python教程99--控制鼠标键盘模块 pyautogui
作者:互联网
1、安装:
pip install pyautogui
官方网站:
pyautoguihttp://pyautogui.readthedocs.io/en/latest/
2、介绍:
PyAutoGUI可以模拟移动鼠标,单击鼠标,用鼠标拖动,按键,按住键,还可以按键盘热键组合。
3、语法集合:
鼠标:
单击:pyautogui.click()
右击:pyautogui.click(button='right')
双击:pyautogui.doubleClick()
移动鼠标:pg.moveTo(100,200,2)
拖拽鼠标:pg.dragTo(300, 400, 2, button='left')
键盘:
回车:pyautogui.press('enter')
按下左键:pyautogui.press('left')
按下CTRL:pyautogui.press('ctrl')
快捷键的话可以使用 hotkey 方法,按下 Ctrl + Shift + T
pyautogui.hotkey('ctrl', 'shift', 't')pyautogui.hotkey('ctrl', 'c')
>>> import pyautogui
# 获取屏幕尺寸
>>> screenWidth, screenHeight = pyautogui.size()
# 获取当前坐标位置
>>> currentMouseX, currentMouseY = pyautogui.position()
# 鼠标移动到坐标为100,150的位置
>>> pyautogui.moveTo(100, 150)
# 鼠标左击
>>> pyautogui.click()
# 鼠标移动10个像素
>>> pyautogui.moveRel(None, 10) # move mouse 10 pixels down
# 鼠标双击
>>> pyautogui.doubleClick()
>>> pyautogui.moveTo(500, 500, duration=2, tween=pyautogui.easeInOutQuad) # use tweening/easing function to move mouse over 2 seconds.
# 键盘输入Hello world! 间隔为0.25秒
>>> pyautogui.typewrite('Hello world!', interval=0.25) # type with quarter-second pause in between each key
# 按键 esc
>>> pyautogui.press('esc')
# shift按下
>>> pyautogui.keyDown('shift')
# 按键左方向键
>>> pyautogui.press(['left', 'left', 'left', 'left', 'left', 'left'])
# shift弹起
>>> pyautogui.keyUp('shift')
# 组合件 ctrl +c
>>> pyautogui.hotkey('ctrl', 'c')
4、其他属性:
PyAutoGUI函数增加延迟为2.5秒:
import pyautogui
pyautogui.PAUSE = 2.5
鼠标操作:
移动
import pyautogui
width, hight = pyautogui.size()
pyautogui.moveTo(width/2, hight/2) # 基本移动
pyautogui.moveTo(200, 200, duration=2) # 移动过程持续2s完成
pyautogui.moveTo(None, 100) # X方向不变,Y方向移动到100
pyautogui.moveRel(-40, 500) # 相对位置移动
PyAutoGUI键盘表:
‘enter’ (或‘return’ 或 ‘\n’) | 回车 |
‘esc’ | ESC键 |
‘shiftleft’, ‘shiftright’ | 左右SHIFT键 |
‘altleft’, ‘altright’ | 左右ALT键 |
‘ctrlleft’, ‘ctrlright’ | 左右CTRL键 |
‘tab’ (‘\t’) | TAB键 |
‘backspace’, ‘delete’ | BACKSPACE 、DELETE键 |
‘pageup’, ‘pagedown’ | PAGE UP 和 PAGE DOWN键 |
‘home’, ‘end’ | HOME 和 END键 |
‘up’, ‘down’, ‘left’, ‘right’ | 箭头键 |
‘f1’, ‘f2’, ‘f3’…. | F1…….F12键 |
‘volumemute’, ‘volumedown’, ‘volumeup’ | 有些键盘没有 |
‘pause’ | PAUSE键 |
‘capslock’, ‘numlock’, ‘scrolllock’ | CAPS LOCK, NUM LOCK, 和 SCROLL LOCK 键 |
‘insert’ | INS或INSERT键 |
‘printscreen’ | PRTSC 或 PRINT SCREEN键 |
‘winleft’, ‘winright’ | Win键 |
‘command’ | Mac OS X command键 |
5、实战:
标签:pyautogui,moveTo,鼠标,python,99,press,100,left 来源: https://blog.csdn.net/itmsn/article/details/122497872