Search code examples
pythonpyautogui

How to get lowercase characters in pyautogui even if capslock is on?


I am using pyautogui.typewrite('text',interval=0.02) for printing but the text comes out in uppercase if capslock is on. Is there any way to get lowercase characters even if capslock is on?


Solution

  • In order to make sure you always write lowecase text, you must do the following:

    1. check the status of CAPSLOCK key
    2. If its turned on, you must turn ti off before do the writing
    3. Force the text to be lowercase and use typewrite method

    In windows, this snipped will do the job:

    import ctypes
    import pyautogui as pya
    
    
    def is_capslock_on():
        return True if ctypes.WinDLL("User32.dll").GetKeyState(0x14) else False
    
    
    def turn_capslock_on():
        if not is_capslock_on():
            pya.press("capslock")
    
    
    def turn_capslock_off():
        if is_capslock_on():
            pya.press("capslock")
    
    
    if __name__ == "__main__":
        import time
    
        turn_capslock_off()
        time.sleep(1)
        the_text = "THIS TEXT will always be writen in LOWERCASE"
        pya.typewrite(the_text.lower(), interval=0.02)