📜  如何使用Python使您的 PC 自动保持唤醒状态?

📅  最后修改于: 2022-05-13 01:55:08.141000             🧑  作者: Mango

如何使用Python使您的 PC 自动保持唤醒状态?

在本文中,我们将讨论如何使用Python使您的 PC 保持唤醒状态。为了执行此任务,我们将使用外部Python模块。

在这里,我们将使用Python包PyAutoGUI (用于以编程方式控制鼠标和键盘的跨平台 GUI 自动化Python模块。)这将使您的 PC 永远保持清醒,直到您关闭程序。使用以下命令安装此模块:

pip install pyautogui

安装模块后执行以下程序:

Python3
# 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')


在执行上述程序时,您将看到在time.sleep()中指定的时间之后,鼠标指针移动到最左角(也可以通过将pyautogui.moveTo()中的坐标提及到您喜欢的 x co 来自定义这一点- 坐标和屏幕的 y 坐标并开始向下移动。