Python uses the PyAutoGUI module to control the mouse and keyboard

        PyAutoGUI is an easy-to-use, cross-platform python third-party library that can simulate keyboard and mouse for automatic operation. This article will explain in detail how it realizes the control of mouse and keyboard. Interested students can learn about it.

        1. Install the pyautogui module using pip:

pip install pyautogui

        This area needs attention: pyautogui has two key hidden dangers when using it (it is difficult to close the program after it is started). One is that an error will appear when the mouse appears at the top of the screen, and the other is that the automatic operation of the keyboard is too fast. , so you need to set the relevant parameters first;

        2. The method of parameter setting is as follows:

import pyautogui as ui  # 导入pyautogui代码块
 
ui.FAILSAFE = False  # 关闭自动防御故障的功能
ui.PAUSE = 1  # 设置键盘操作的时候停顿间隔时间为1秒

        3. Mouse related controls:

'''获取屏幕分辨率'''
print(ui.size())
# Size(width=1920, height=1080)
# 屏幕分标率:宽:1920px、高:1080px
 
'''移动鼠标到指定位置'''
ui.moveTo(250, 400, duration=1)  # duration 参数设置移动到指定位置的时间为1秒钟
 
'''按方向移动鼠标'''
ui.moveRel(200, 300, duration=2)  # 鼠标向右移动200px、向下移动300px,移动时间为2秒钟
 
'''获取当前鼠标的位置'''
print(ui.position())
# Point(x=450, y=700)
 
'''单击鼠标'''
ui.click(200, 200, button='left')  # 在屏幕200px,200px的位置点击鼠标左键
ui.click(200, 200, button='right')  # 在屏幕200px,200px的位置点击鼠标右键
ui.click(200, 200, button='middle')  # 在屏幕200px,200px的位置点击鼠标中键
 
'''双击鼠标'''
ui.doubleClick(200, 200)  # 在屏幕200px,200px的位置双击鼠标左键
ui.rightClick(200, 200)  # 在屏幕200px,200px的位置双击鼠标右键
ui.middleClick(200, 200)  # 在屏幕200px,200px的位置双击鼠标中键
 
'''按下与释放鼠标'''
ui.mouseDown()  # 按下鼠标
ui.mouseUp()  # 释放鼠标
 
'''拖动鼠标'''
ui.dragTo(200, 300, duration=1)  # 拖动鼠标到指定位置
ui.dragRel(200, 300, duration=4)  # 按方向拖动鼠标
 
'''滚动鼠标'''
ui.scroll(450)  # 向上滚动450个像素单位

        4. Keyboard-related controls:

'''
pyautogui.keyDown() : 模拟按键按下;
pyautogui.keyUp() : 模拟按键释放;
pyautogui.press() : # 就是调用keyDown() & keyUp(),模拟一次按键;
pyautogui.typewrite('this',0.5) : 第一参数是输入内容,第二个参数是每个字符间的间隔时间;
pyautogui.typewrite(['T','h','i','s']):typewrite 还可以传入单字母的列表;
'''
 
ui.keyDown('ctrl')  # 按下ctrl键
ui.press('Y')  # 按一下Y键
ui.keyUp('ctrl')  # 释放ctrl键
 
ui.typewrite('YYDS', 0.5)  # 输入YYDS的字符串,每个字符之间的时间间隔是0.5秒
 
'''一些特定键的使用'''
 
# 可以使用typewrite函数调用类似于enter的特殊按键
ui.typewrite(['enter'])  # 模拟按一次enter键
 
'''
键盘字符串   说明
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…. f12    F1…….F12键
volumemute, volumedown,volumeup 声音变大变小静音(有些键盘没有)
pause   PAUSE键,暂停键
capslock    CAPS LOCK 键
numlock NUM LOCK 键
scrolllock  SCROLLLOCK 键
insert  INSERT键
printscreen PRINT SCREEN键
winleft, winright   Win键(windows )
command command键(Mac OS X )
option  option(Mac OS X)
'''

        You can use the typewrite function to process special keys according to the strings corresponding to the above key processing list:

ui.typewrite(['esc'])  # 模拟按一下esc键
 
'''快捷键的模拟处理'''
 
# 程序猿神器:CV大法
ui.hotkey('ctrl','c')  # ctrl + c
ui.hotkey('ctrl','v')  # ctrl + v

        You can also use the traditional method to press the keys one by one, and finally combine the shortcut keys:

# 模拟代码格式化快捷键ctrl + alt +l
ui.keyDown('ctrl')
ui.keyDown('alt')
ui.keyDown('l')
 
# 挨个释放这几个键
ui.keyUp('l')
ui.keyUp('alt')
ui.keyUp('ctrl')

        If you debug the code in the windows environment, you need to run the compiler as an administrator.

Guess you like

Origin blog.csdn.net/2203_75758128/article/details/130785214