Last active
January 28, 2022 17:14
-
-
Save shashankm28/9298fd49ec8c51ec988254a16b8b981e to your computer and use it in GitHub Desktop.
Prevent Windows from locking by simulating keypress | Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## This script will simulate a keypress and prevent Windows from locking | |
import pyautogui | |
import time | |
def no_lock(button): | |
try: | |
print ('Press CTRL+C to stop.') | |
while True: | |
pyautogui.press(button) # Key pressed | |
time.sleep(2) | |
except Exception as ex: | |
print ('no_lock | Error: ', ex) | |
def main(): | |
try: | |
print ('\nPrevent Windows screenlock') | |
kb_button = str(input('Enter keyboard button: ')) | |
print ('\nRunning') | |
no_lock(kb_button) | |
except KeyboardInterrupt: | |
print('\nStopped') | |
except Exception as ex: | |
print ('main | Error: ', ex) | |
if __name__ == "__main__": | |
main() |
Rutrus
commented
Jan 28, 2022
Thanks for reminding me, the second,
pyautogui.keyUp(button) # Key released
is not required, it was a stupid mistake from my side (didn't go through the documentation completely).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment