微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python控制鼠标和键盘-PyAutoGUI用法详解

pyautogui——让所有GUI都自动

安装代码

pip install pyautogui

1.简介

1.1 目的

pyautogui一个纯Python的GUI自动化工具,其目的是可以用程序自动控制鼠标和键盘操作,多平台支持(Windows,OS X,Linux)。可以用pip安装,Github上有源代码

下面的代码让鼠标移到屏幕中央。

import pyautogui
screenWidth, screenHeight = pyautogui.size()
pyautogui.moveto(screenWidth / 2, screenHeight / 2)

pyautogui可以模拟鼠标的移动、点击、拖拽,键盘按键输入、按住操作,以及鼠标+键盘的热键同时按住等操作,可以说手能动的都可以。

pyautogui基础操作样例

import pyautogui获取当前屏幕分辨率
screenWidth, screenHeight = pyautogui.size()

# 获取当前鼠标位置
currentMouseX, currentMouseY = pyautogui.position()

# 鼠标移动坐标为100,100位置  绝对移动
pyautogui.moveto(100, 100)

# 鼠标左击
pyautogui.click()

# 鼠标乡下移动  相对移动
pyautogui.moveRel(None, 10)

# 鼠标双击
pyautogui.doubleClick()

#  用缓动/渐变函数让鼠标2秒后移动到(500,500)位置
#  use tweening/easing function to move mouse over 2 seconds.
pyautogui.moveto(500, 500, duration=2, tween=pyautogui.easeInOutQuad)

#  在每次输入之间暂停0.25秒
pyautogui.typewrite('Hello world!', interval=0.25)

# 键盘点击esc
pyautogui.press('esc')

# 按住shift键
pyautogui.keyDown('shift')
pyautogui.press(['left', 'left', 'left'])

# 放开shift键
pyautogui.keyUp('shift')
pyautogui.hotkey('ctrl', 'c')


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 LOCKNUM LOCK,和 SCROLL LOCK 键

‘insert’

INSINSERT键

‘printscreen’

PRTSC 或 PRINT SCREEN键

‘winleft’‘winright’

Win键

‘command’

Mac OS X command键

文档:

https://muxuezi.github.io/posts/doc-pyautogui.html

http://pyautogui.readthedocs.io/en/latest/introduction.html

http://blog.csdn.net/ibiao/article/details/54406803

http://www.chenxm.cc/post/633.html


版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐