463 views|3 replies

538

Posts

3

Resources
The OP
 

Reading notes on the book "Python Programming Quick Start (2nd Edition)" 12. Using GUI Automation to Control the Keyboard and Mouse (2) [Copy link]

This post was last edited by xinmeng_wit on 2024-6-7 20:35

In addition to getting the screen size and moving the mouse pointer mentioned in the previous article, the pyautogui module has many other functions. This article continues to explore fun and interesting content.

Since we can move the mouse pointer, we can definitely click the mouse, drag the mouse, and scroll the mouse wheel.

Clicking the mouse can be achieved by calling the pyautogui.click() method, which specifies the click position coordinates and key (left, middle, right).

You can also call pyautogui.doubleClick() to double-click, pyautogui.rightClick() to right-click, and pyautogui.middleClick() to click the scroll wheel.

In addition, the pyautogui module can also realize mouse dragging by calling pyautogui.dragTo() and pyautogui.drag() methods. Dragging means pressing the left mouse button to move the mouse position.

The pyautogui.scroll() method is used to scroll the mouse, and the number of units to scroll can be specified by parameters.

Next, we will use a program example to illustrate these methods.

import pyautogui, time

# 单击鼠标,位置(10 , 5)
pyautogui.click(10, 5)
# 延时5s
time.sleep(5)
# 单击,激活窗口
pyautogui.click()

# 在画板上画图
distance = 300
change = 20
while distance > 0:
    pyautogui.drag(distance, 0, duration=0.2) # 鼠标向右拖动
    distance = distance - change
    pyautogui.drag(0, distance, duration=0.2) # 鼠标向下拖动
    pyautogui.drag(-distance, 0, duration=0.2) # 鼠标向左拖动
    distance = distance - change
    pyautogui.drag(0, -distance, duration=0.2) # 鼠标向上拖动

# 滚动鼠标
pyautogui.scroll(200)

The graphics drawn by the above program on the drawing board are as follows. The brushes selected in the left and right pictures are different, which is quite interesting.

In addition to the above, pyautogui can also obtain the RGB value of the specified position on the screen, and determine whether the RGB value is equal to a given value. This method can be used to identify the color of the button. If the color of the button matches the expected color, the click operation can be performed. If it does not match, it means that the window position has moved and cannot be clicked.

import pyautogui

# 获取(0,0)位置的颜色
print(pyautogui.pixel(0,0))
# 匹配(0,0)位置的颜色,如果匹配则返回True,如果不匹配返回False
print(pyautogui.pixelMatchesColor(0,0,(125, 182, 191)))

operation result:

In order to facilitate the Python program to identify the window to be clicked, the pyautogui module provides some interfaces for obtaining window information and operating windows.

The detailed interface description has been written in the code comments, so I won’t explain it in detail.

import pyautogui,time

# 获取当前活动窗口
fw = pyautogui.getActiveWindow()

# 打印窗口信息
print(str(fw))
# 获取title
print(fw.title)
# 获取窗口大小
print(fw.size)

# 调整窗口宽度为800
fw.width = 800

# 窗口是否最大化,返回True/False
print(fw.isMaximized)

# 窗口是否最小化,返回True/False
print(fw.isMinimized)

# 窗口是否激活,返回True/False
print(fw.isActive)

# 最大化窗口
fw.maximize()

# 撤销最大化操作
fw.restore()

# 等待5s
time.sleep(5)

# 关闭窗口
fw.close()

operation result:

After waiting for 5 seconds, the window closed automatically.

In addition to operating the mouse and window, the last part can also operate the keyboard and simulate keyboard pressing. This function can realize program-controlled typing.

The simulated key interface is very simple. You only need to call the pyautogui.write() method to input any key.

We can use the Python program to automatically type the string "Hello world!" in Notepad. After running the program, remember to put the cursor on the Notepad interface.

import pyautogui,time
# 等待5s,用于准备
time.sleep(5)

# 模拟键盘输入
pyautogui.write('Hello world!', 0.25)

After running the above program, the corresponding string will be printed in Notepad.

In addition to these simple letters, the pyautogui module can also simulate the Enter key ('enter'), the ESC key ('esc'), the Shift key, and more.

The character/string list in pyautogui corresponding to various keys is as follows:

In addition to the pyautogui.write() method that can simulate key signals, there are several methods that can also simulate the behavior of pressing and releasing keys, such as pyautogui.keyDown(), pyautogui.keyUp(), pyautogui.press()

pyautogui.keyDown() means that the key is pressed and not released, pyautogui.keyUp() means that the key is released, and pyautogui.press() means that the key is pressed and then released.

These three methods are very useful for simulating keyboard key combinations.

Let’s look at an example:

import pyautogui,time

pyautogui.keyDown('shift')
pyautogui.press('4')
pyautogui.keyUp('shift')

Running the above code will output the dollar sign, simulating the shift + 4 key combination.

It is still very troublesome to implement key combinations in this way, because you have to write multiple lines of code. The author of the pyautogui module should have considered this problem, so he provided a method specifically for simulating key combinations, which is the pyautogui.hotkey() method.

This method only needs to pass in the corresponding key values in sequence to complete the pressing and releasing of the combination key, which is very convenient.

For example, the following line of code can simulate pressing the ctrl + c key combination on the keyboard.

pyautogui.hotkey('ctrl', 'c')

Isn’t it very convenient?

Well, this is the end of this chapter on GUI automation to control the mouse and keyboard. The content of this chapter is actually very interesting, and it provides a lot of inspiration and help to friends who use Python for GUI automation. I also hope that you will have the opportunity to use it in your own projects in the future, improve work efficiency, free your hands, get out of the sea of overtime as soon as possible, get off work on time, and enjoy life!!!

Latest reply

The technical content shared by the host of this post on how to automatically control the mouse and keyboard using Python GUI is worth learning. I feel that this technology has a lot of room for application.   Details Published on 2024-6-10 08:46
 
 

6818

Posts

11

Resources
2
 

Well, this is the end of this chapter on GUI automation to control the mouse and keyboard. The content of this chapter is actually very interesting, and it provides a lot of inspiration and help to friends who use Python for GUI automation. I also hope that you will have the opportunity to use it in your own projects in the future, improve work efficiency, free your hands, get out of the sea of overtime as soon as possible, get off work on time, and enjoy life!!!

Indeed, after learning and mastering knowledge, it is necessary to use it in reality. Otherwise, it loses its meaning!

 
 
 

719

Posts

4

Resources
3
 

The technical content shared by the host of this post on how to automatically control the mouse and keyboard using Python GUI is worth learning. I feel that this technology has a lot of room for application.

Comments

It can free your hands, realize automatic clicking, and automatic key input, which is quite convenient.  Details Published on 2024-6-11 10:23
 
 
 

538

Posts

3

Resources
4
 
chejm posted on 2024-6-10 08:46 The technical content of python GUI automatic control of mouse and keyboard shared by the OP is worth learning. I feel that this technology has a lot of application space

It can free your hands, realize automatic clicking, and automatic key input, which is quite convenient.


 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list