How to Blink Caps Lock using Python Code

Automate the Caps Key of Your Keyboard. Blink the Caps Key indicator for a number of times using a Python Program.

Blink caps lock using python code

After pressing the caps lock key usually we see that a keyboard indicator light turns on and off. It happens manually. 

Let's try something unique. In this tutorial, we will see how to blink the caps lock key indicator automatically without touching it. I have made a program using a few lines of python code. It will automate the caps key of your keyboard.

In this code, I have mentioned how many times the key indicator will turn on and off. You can modify the number as per your choice.

👉Visit Also: How to Make a simple Keylogger in Python

How to blink caps lock using python code - PySeek
Image by Kamalakannan PM from Pixabay 

Libraries You need to Install

Installation Process

  • pip install pynput

  • pip install python-xlib

Code

from pynput.keyboard import Key, Controller
from Xlib import display
import time

# Initializing a variable
count = 0

def light_control():
global count # It's for update count variable globally
keyboard = Controller()
# Wait for 0.5 sec.
time.sleep(0.5)

# This part checks capslock is on or off
DISPLAY = display.Display()
keyb = DISPLAY.get_keyboard_control()
DATA = keyb._data
led = DATA['led_mask']
caps= (led & 1) == 1 #(caps:bool) type

if count < 20:
if caps: # if caps == 1, caps is ON
keyboard.press(Key.caps_lock)
keyboard.release(Key.caps_lock)
print('caps is OFF')
else: # caps == 0, caps is OFF
keyboard.press(Key.caps_lock)
keyboard.release(Key.caps_lock)
print("caps is ON")
# Increasing count variable by 1
count += 1

# Recursively calling the function to blink the
# light frequently
light_control()

if __name__ == "__main__":
#Calling the function
light_control()

Output

🧐Visit AlsoGet Hardware and System Information Using Python

Conclusion

In the code above, the Caps Lock LED has been turned on and off 20 times. You can set the number as per your requirement by changing the value given in the if statement.

You can get more lovely unique programming examples from here. Visit those also. I hope You loved this tutorial. Please share Your love❤️ and drop Your comment below. You'll get a reply soon.

Thanks for reading!💙

PySeek

Subhankar Rakshit

Meet Subhankar Rakshit, a Computer Science postgraduate (M.Sc.) and the creator of PySeek. Subhankar is a programmer, specializes in Python language. With a several years of experience under his belt, he has developed a deep understanding of software development. He enjoys writing blogs on various topics related to Computer Science, Python Programming, and Software Development.

Post a Comment (0)
Previous Post Next Post