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

Question:

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?

Asked By: Adii_Mathur

||

Answers:

The key codes a keyboard generates are always case insensitive, the state of Caps Lock and Shift determines whether keys will appear upper or lower case.

To achieve lower case characters when Caps Lock is on you’ll have to use shift. For example:

pyautogui.keyDown('shift')
pyautogui.typewrite('text',interval=0.02)
pyautogui.keyUp('shift')

As far as I know pyautogui currently doesn’t provide a method to determine the current state of caps lock, just like it can’t detect the state of any key at all. (Planned support is listed on the road map though, see https://pyautogui.readthedocs.io/en/latest/roadmap.html .)

Answered By: Elijan9

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)
Answered By: Bravhek
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.