Skip to content

Instantly share code, notes, and snippets.

@shashankm28
Last active January 28, 2022 17:14
Show Gist options
  • Save shashankm28/9298fd49ec8c51ec988254a16b8b981e to your computer and use it in GitHub Desktop.
Save shashankm28/9298fd49ec8c51ec988254a16b8b981e to your computer and use it in GitHub Desktop.
Prevent Windows from locking by simulating keypress | Python
## 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
Copy link

Rutrus commented Jan 28, 2022

pyautogui.keyDown(button)  # Key pressed
time.sleep(2)
pyautogui.keyUp(button)     # Key released

@shashankm28
Copy link
Author

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