3.1 屏幕和鼠标位置
X坐标从左侧的0开始,向右增加,Y坐标从顶部的0开始,向下递增。
左上角的像素位于坐标0,0。如果屏幕分辨率为1920 x 1080,则右下角的像素将为1919,1079(因为坐标从0开始,而不是1)。文章来源:https://www.toymoban.com/news/detail-490754.html
3.1.1 输出屏幕分辨率大小与鼠标光标位置
import pyautogui
# size():以两个整数的元组形式返回屏幕分辨率大小。
Screen_size = pyautogui.size()
print("当前屏幕大小为:",Screen_size)
# position():返回鼠标光标的当前X和Y坐标
print('Press Ctrl-C to quit.')
try:
while True:
# 获取当前鼠标光标位置
x, y = pyautogui.position()
# print(x)
# print(y)
# rjust()返回长宽右对齐的字符串。
positionStr = 'X: ' + str(x).rjust(4) + ' Y: ' + str(y).rjust(4)
print(positionStr, end='')
print('\b' * len(positionStr), end='', flush=True)
except KeyboardInterrupt:
print('\n')
文章来源地址https://www.toymoban.com/news/detail-490754.html
3.1.2 判断位置是否在屏幕内
import pyautogui
FLag = pyautogui.onScreen(0, 0) # True
FLag = pyautogui.onScreen(0, -1) # False
FLag = pyautogui.onScreen(0, 99999999) # False
FLag = pyautogui.onScreen(1920, 1080) # False
FLag = pyautogui.onScreen(1919, 1079) # True
3.2 鼠标均速移动(相对位置与绝对位置)
3.2.1 moveTo()将鼠标光标移动到X和Y整数坐标(绝对位移)
import pyautogui
# moveTo()函数将鼠标光标移动到X和Y整数坐标,为None时为当前坐标
# 当前坐标为(600,300)
pyautogui.moveTo(100, 200) # 将光标瞬间移动到(100, 200)位置
pyautogui.moveTo(None, 500) # 将光标瞬间移动到(600, 200)位置
pyautogui.moveTo(600, None) # 将光标瞬间移动到(100, 300)位置
# 如果持续时间小于pyautogui.MINIMUM_duration,则移动将是即时的。
# 默认情况下,pyautogui.MINIMUM_DURATION为0.1
pyautogui.moveTo(100, 200, 2) # 用时2秒将光标移动到(100, 200)位置
3.2.2 move()将鼠标光标移动到相对于其当前位置的几个像素(相对位移)
import pyautogui
# 相对位移
# move()将鼠标光标移动到相对于其当前位置的几个像素上
pyautogui.moveTo(100, 200)
pyautogui.move(0, 50) # 移动到(100, 250)
pyautogui.move(-30, 0) # 移动到(70, 250)
pyautogui.move(-30, None) # 移动到(40, 250)
3.3 鼠标拖动
import pyautogui
# 按住鼠标左键的同时,将光标拖到(100, 200)位置
pyautogui.dragTo(100, 200, button='left')
# 按住鼠标左键的同时,用2秒将光标拖到(300, 400)位置
pyautogui.dragTo(300, 400, 2, button='left')
# 按住鼠标右键的同时,用2秒将光标拖到相对于当前位置的(30, 0)位置
pyautogui.drag(30, 0, 2, button='right')
3.4 鼠标随机速度移动
import pyautogui
# 在一段时间内移动鼠标时,鼠标会以恒定速度直线直接移动到目标位置。这被称为线性二元函数或线性宽松函数。
# PyAutoGUI的tweening函数可以随机调节速度。
# https://pypi.python.org/pypi/ PyTweening https://github.com/asweigart/pytweening
# 如果要创建自己的tweening函数,请定义一个函数,该函数接受一个介于0.0(表示鼠标移动的开始)和1.0(表示鼠标运动的结束)之间的浮点参数,并返回介于0.0和1.0之间的浮点值。
# 使鼠标光标开始缓慢移动,然后加速向目标移动。
pyautogui.moveTo(100, 100, 2, pyautogui.easeInQuad) # 先慢后快,用时2秒
# easeOutQuad则相反:鼠标光标开始快速移动,但在接近目标时会减慢速度。
pyautogui.moveTo(100, 100, 2, pyautogui.easeOutQuad) # 先快后慢,用时2秒,
pyautogui.moveTo(100, 100, 2, pyautogui.easeInOutQuad) # 开始结束快,中间慢
pyautogui.moveTo(100, 100, 2, pyautogui.easeInBounce) # bounce at the end
pyautogui.moveTo(100, 100, 2, pyautogui.easeInElastic) # rubber band at the end
3.5 鼠标点击
import pyautogui
# click()函数模拟在鼠标当前位置单击鼠标左键。“点击”是指按下按钮,然后向上释放。
pyautogui.click()
# 移动到(100,200)位置,并左键1次
pyautogui.click(x=100, y=200)
# 鼠标右键1次
pyautogui.click(button='right')
# 鼠标左键2次
pyautogui.click(clicks=2)
# 鼠标左键2次,中间有0.15秒的停顿
pyautogui.click(clicks=2, interval=0.25)
# 鼠标右键三次,每次中间有0.15秒的停顿
pyautogui.click(button='right', clicks=3, interval=0.25)
# 鼠标左键2次,可增参数 x y
pyautogui.doubleClick()
# 鼠标左键3次,可增参数 x y
pyautogui.tripleClick()
# 鼠标右键1次,可增参数 x y
pyautogui.rightClick()
3.6 鼠标按下与抬起
import pyautogui
# 鼠标单击和拖动包括按下鼠标按钮和释放鼠标按钮
# 鼠标左键按下
pyautogui.mouseDown()
# 鼠标左键抬起
pyautogui.mouseUp()
# 鼠标右键按下
pyautogui.mouseDown(button='right')
# 鼠标右键在(100,200)位置抬起
pyautogui.mouseUp(button='right', x=100, y=200)
3.7 鼠标滚动scroll()
# 可以通过调用scroll()函数并传递整数次clicks”来模拟鼠标滚轮。“click”中的滚动量因平台而异。
# 可以为x和y关键字参数传递整数,以便在执行滚动之前移动鼠标光标。
import pyautogui
# scroll():整数次clicks来模拟鼠标滚轮。“click”中的滚动量因平台而异。
# 向上滚动10次“点击”
pyautogui.scroll(10)
# 向下滚动10次“点击”
pyautogui.scroll(-10)
# 先移动到(100,100)位置,再向上滚动10次“点击”
pyautogui.scroll(10, x=100, y=100)
4 键盘控制功能
4.1 write()函数
import pyautogui
# 使用write()只能按单字符键,因此不能按Shift 或 F1键
# 立即输入字符
pyautogui.write('Hello world!')
# 依次输入字符,每个字符之间间隔0.25秒
pyautogui.write('Hello world!', interval=0.25)
4.2 press()、keyDown()和keyUp()函数
import pyautogui
# press()函数实际上只是keyDown()和keyUp()函数的包装器,它们模拟按下一个键,然后释放它。
# 在每次按下之间添加延迟间隔,设置interval即可,传入int或float
# 按下回车键
pyautogui.press('enter')
# 按下F1键
pyautogui.press('f1')
# 按下←键
pyautogui.press('left')
# 要在按住Shift键的同时按左箭头键三次
pyautogui.keyUp('shift')
pyautogui.press(['left', 'left', 'left']) # 多个按键用字符串列表
pyautogui.keyDown('shift')
# 设置presses=3,实现按←键3次
pyautogui.press('left', presses=3)
4.3 hotkey() :实现热键组合
import pyautogui
# 为了方便按下热键或键盘快捷键,可将热键()传递给几个键串,这些键串将按顺序按下,然后按相反顺序释放。
# 要在每次按下之间添加延迟间隔,可为interval参数传递int或float
pyautogui.hotkey('ctrl', 'shift', 'esc')
# 等价于
pyautogui.keyDown('ctrl')
pyautogui.keyDown('shift')
pyautogui.keyDown('esc')
pyautogui.keyUp('esc')
pyautogui.keyUp('shift')
pyautogui.keyUp('ctrl')
4.4 KEYBOARD_KEYS
KEY_NAMES = [
"\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",
]
到了这里,关于【PyAutoGUI操作指南】02 鼠标控制功能+获取当前坐标+鼠标事件+鼠标滚动查询的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!