简体   繁体   English

如何在没有按键的情况下更改大写锁定状态

[英]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. 我正在使用按下Caps Lock键时激活的python程序,我希望能够在程序处于活动状态时打开/关闭大写锁定状态。

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. 我尝试使用virtkey发送密钥,但它显然不起作用,因为密钥只是激活应用程序而不更改大写锁定状态。 So what is the best way to achieve this with python? 那么用python实现这一目标的最佳方法是什么?

I'm using Ubuntu 我正在使用Ubuntu

On Linux: 在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 来源: Benji York - Stack Overflow:在Python中更改键盘锁


On Windows: 在Windows上:

You should be able to use SendKeys for this, as in the following example: 您应该可以使用SendKeys ,如下例所示:

import SendKeys

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

Use sendkeys to change the status and keyboardleds to change the LED indicators. 使用sendkeys更改状态和keyboardleds以更改LED指示灯。

sendkeys: 的SendKeys:

From another SO dicussion : 另一个SO裁决

import SendKeys

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

keyboardleds: keyboardleds:

This package seems to work only for POSIX (which is OK if you're using Ubuntu), and you can read more here . 这个包似乎只适用于POSIX(如果你使用的是Ubuntu就可以了),你可以在这里阅读更多内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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