简体   繁体   中英

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

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}
""")

Use sendkeys to change the status and keyboardleds to change the LED indicators.

sendkeys:

From another SO dicussion :

import SendKeys

SendKeys.SendKeys("""
{CAPSLOCK}
{SCROLLOCK}
{NUMLOCK}
""")

keyboardleds:

This package seems to work only for POSIX (which is OK if you're using Ubuntu), and you can read more here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM