Chapter 18 – Controlling the Keyboard and Mouse with GUI Automation
非计算机出身,最近一直在看《Automate the Boring Stuff with Python》,为了能够很好的理解和学习,一直坚持在做文后的练习题。但一个人学习实在是太累太难,准备将自己的做的练习题记录下来,希望得到高手的指导,来拓展个人解决问题的思路、提升代码质量。
传送门:
Chapter 12 – Working with Excel Spreadsheets
Chapter 13 – Working with PDF and Word Documents
Chapter 14 – Working with CSV Files and JSON Data
Chapter 15 – Keeping Time, Scheduling Tasks, and Launching Programs
Chapter 17 – Manipulating Images
Looking Busy
Many instant messaging programs determine whether you are idle, or away from your computer, by detecting a lack of mouse movement over some period of time—say, ten minutes. Maybe you’d like to sneak away from your desk for a while but don’t want others to see your instant messenger status go into idle mode. Write a script to nudge your mouse cursor slightly every ten seconds. The nudge should be small enough so that it won’t get in the way if you do happen to need to use your computer while the script is running.
我的解决思路是让鼠标的y-coordinate向下移动一个像素,然后再移动回来,确保鼠标不会远离当前的绝对坐标值。循环执行,直到人为的中断(Ctrl+C)执行。
#! python3
# lookingBusy.py
import pyautogui
import time
try:
while True:
time.sleep(10)
x, y = pyautogui.position()
print('X: ' + str(x) + ' Y:' + str(y))
pyautogui.moveTo(x, y+1)
x, y = pyautogui.position()
print('X: ' + str(x) + ' Y:' + str(y))
pyautogui.moveTo(x, y-1)
except KeyboardInterrupt:
print('\nDone')