How to show the status of CAPS Lock Key in tkinter?

We can use the <Lock-KeyPress> and <Lock-KeyRelease> bindings to check if the CAPS Lock Key is ON or off. In the following example, we will create two user-defined functions "caps_lock_on()" and "caps_lock_off()" which will capture the event of Lock-KeyPress and Lock-KeyRelease and display the status on the screen.

Example

Here's a complete example that shows the CAPS Lock status using event bindings ?

# Import required libraries
from tkinter import *

# Create an instance of tkinter frame
win = Tk()

# Define the geometry of the window
win.geometry("700x250")
win.title("CAPS Lock Status")

def caps_lock_on(e):
    label_caps.config(text="CAPS Lock is ON", fg="red")

def caps_lock_off(e):
    label_caps.config(text="CAPS Lock is OFF", fg="green")

# Create a label to display the status
label_caps = Label(win, text="Press CAPS Lock to see status", font="Helvetica 15 bold")
label_caps.pack(pady=20)

# Bind the CAPS Lock events
win.bind("<Lock-KeyPress>", caps_lock_on)
win.bind("<Lock-KeyRelease>", caps_lock_off)

# Start the GUI
win.mainloop()

How It Works

The program works by binding two specific events to the main window:

  • <Lock-KeyPress> − Triggered when CAPS Lock is turned on
  • <Lock-KeyRelease> − Triggered when CAPS Lock is turned off

When these events occur, the corresponding functions update the label text and color to reflect the current CAPS Lock status.

Enhanced Version with Visual Feedback

Here's an improved version with better visual feedback and initial status detection ?

import tkinter as tk

class CapsLockStatus:
    def __init__(self):
        self.root = tk.Tk()
        self.root.geometry("400x200")
        self.root.title("CAPS Lock Status Monitor")
        self.root.configure(bg="lightgray")
        
        # Main frame
        frame = tk.Frame(self.root, bg="lightgray")
        frame.pack(expand=True, fill="both", padx=20, pady=20)
        
        # Title label
        title = tk.Label(frame, text="CAPS Lock Status Monitor", 
                        font=("Arial", 16, "bold"), bg="lightgray")
        title.pack(pady=(0, 20))
        
        # Status label
        self.status_label = tk.Label(frame, text="Press CAPS Lock to see status", 
                                   font=("Arial", 14, "bold"), 
                                   bg="white", relief="sunken", 
                                   width=25, height=3)
        self.status_label.pack(pady=10)
        
        # Instructions
        instructions = tk.Label(frame, text="Press CAPS Lock key to toggle status", 
                              font=("Arial", 10), bg="lightgray", fg="gray")
        instructions.pack(pady=(10, 0))
        
        # Bind events
        self.root.bind("<Lock-KeyPress>", self.caps_on)
        self.root.bind("<Lock-KeyRelease>", self.caps_off)
        
        # Make window focusable
        self.root.focus_set()
    
    def caps_on(self, event):
        self.status_label.config(text="? CAPS Lock is ON", 
                               bg="lightcoral", fg="darkred")
    
    def caps_off(self, event):
        self.status_label.config(text="? CAPS Lock is OFF", 
                               bg="lightgreen", fg="darkgreen")
    
    def run(self):
        self.root.mainloop()

# Create and run the application
app = CapsLockStatus()
app.run()

Key Features

  • Event Binding: Uses <Lock-KeyPress> and <Lock-KeyRelease> events
  • Visual Feedback: Changes background color and text when status changes
  • User-Friendly: Clear instructions and emoji indicators
  • Real-time Updates: Status updates immediately when CAPS Lock is toggled

Conclusion

Monitoring CAPS Lock status in tkinter is straightforward using Lock-KeyPress and Lock-KeyRelease event bindings. The enhanced version provides better visual feedback with colors and icons for improved user experience.

Updated on: 2026-03-26T18:37:27+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements