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?
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 .)
In order to make sure you always write lowecase text, you must do the following:
- check the status of CAPSLOCK key
- If its turned on, you must turn ti off before do the writing
- 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)
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?
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 .)
In order to make sure you always write lowecase text, you must do the following:
- check the status of CAPSLOCK key
- If its turned on, you must turn ti off before do the writing
- 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)