Welcome folks today in this blog post we will be preventing
our pc from sleeping and awaking it automatically
using pyautogui library in python. All the full source code of the application is given below.
Get Started
In order to get started you need to install the following library using the pip
command as shown below
pip install pyautogui
After installing this library make an app.py
file and copy paste the following code
app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# Import required modules import pyautogui import time # FAILSAFE to FALSE feature is enabled by default # so that you can easily stop execution of # your pyautogui program by manually moving the # mouse to the upper left corner of the screen. # Once the mouse is in this location, # pyautogui will throw an exception and exit. pyautogui.FAILSAFE = False # We want to run this code for infinite # time till we stop it so we use infinite loop now while True: # time.sleep(t) is used to give a break of # specified time t seconds so that its not # too frequent time.sleep(15) # This for loop is used to move the mouse # pointer to 500 pixels in this case(5*100) for i in range(0, 100): pyautogui.moveTo(0, i * 5) # This for loop is used to press keyboard keys, # in this case the harmless key shift key is # used. You can change it according to your # requirement. This works with all keys. for i in range(0, 3): pyautogui.press('shift') |
And now if you execute the above script
by typing the below command
python app.py
Now if you execute this after some time you haven’t done anything on your pc with your mouse
and keyboard
this will automatically move your mouse
after a specified amount of time