pause loop when capslock is down and up in python

You can use the keyboard library to check whether the caps lock key is pressed or not, and use pyautogui library to pause the loop. Here's the code:

main.py
import keyboard
import pyautogui

while True:
    if keyboard.is_pressed('caps lock'):
        pyautogui.PAUSE = 1
        print('Caps Lock is down')
    else:
        pyautogui.PAUSE = 0
        print('Caps Lock is up')
221 chars
11 lines

In this code, we are using an infinite while loop and checking if the caps lock key is pressed. If it is, we set the pyautogui.PAUSE variable to 1 (which means a 1-second delay between each pyautogui function call), and print 'Caps Lock is down'. If the caps lock key is not pressed, we set the pyautogui.PAUSE variable to 0 (which means no delay), and print 'Caps Lock is up'.

gistlibby LogSnag