Python Forum

Full Version: Check for Caps Lock
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

In a class I use the code below to print in the console if the Caps Lock is on or not.
Instead of printing, I'd like to compare the e.state and if it's =="Caps Lock" then dispaly a msg:
messagebox.showinfo ( "Warning" , "Caps Lock is activated")

How do I do that?
TIA


mods = {
    0x0001: 'Shift',
    0x0002: 'Caps Lock',
    0x0004: 'Control',
    0x0008: 'Left-hand Alt',
    0x0010: 'Num Lock',
    0x0080: 'Right-hand Alt',
    0x0100: 'Mouse button 1',
    0x0200: 'Mouse button 2',
    0x0400: 'Mouse button 3'
}
self.username.bind('<Key>', lambda e: print('Mods:', mods.get( e.state, None )))
you can also use a keyboard listener such as https://pypi.org/project/keyboard-listener/
there are many to choose from: https://pypi.org/search/?q=keyboard+%2B+listener