Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change caps lock status without key press

I am using a python program that is activate when pressing Caps Lock key and I want to be able to turn on/off the caps lock status when the program is active.

I tried to send keys with virtkey but it obviously don't work since the keys just activate the app and don't change the caps lock status. So what is the best way to achieve this with python?

I'm using Ubuntu

like image 546
Anderson Santos Avatar asked Jan 31 '10 11:01

Anderson Santos


People also ask

How do I turn off Caps Lock without the button?

Click Advanced settings on the left side of the window. Scroll to find and open Change language bar hot keys. Switch to the Advanced Key Settings tab. Check the Press the SHIFT key box under To turn off Caps Lock.

How do I turn my Caps Lock back to normal?

I accidentally reversed the function of the Caps Lock key by pressing Ctrl+Shift+Caps Lock. Pressing this combination of keys again corrects the problem. Was this reply helpful? If you are distracted by it, right click on the Status bar in Word and uncheck the box for Caps Lock.

How do I change my Caps Lock notification?

Hit the Windows key & type: Control Panel and then open it. Then steer to the Key Settings tab & double-click on Caps Lock. Now uncheck 'Displays Caps Lock Status on Screen' & reboot your PC. Upon reboot, check if the system is clear of the caps lock notification.


1 Answers

On Linux:

import fcntl
import os

KDSETLED = 0x4B32

console_fd = os.open('/dev/console', os.O_NOCTTY)

# Turn on caps lock
fcntl.ioctl(console_fd, KDSETLED, 0x04)

# Turn off caps lock
fcntl.ioctl(console_fd, KDSETLED, 0)

Source: Benji York - Stack Overflow: Change keyboard locks in Python


On Windows:

You should be able to use SendKeys for this, as in the following example:

import SendKeys

SendKeys.SendKeys("""
{CAPSLOCK}
""")
like image 147
Daniel Vassallo Avatar answered Sep 20 '22 04:09

Daniel Vassallo