简体   繁体   English

有谁知道如何在Python中打开/关闭大写锁定?

[英]Anybody know how to toggle caps lock on/off in Python?

I'm trying to toggle caps lock on/off when the two shift buttons are held down for a second. 当两个按钮按住一秒钟时,我正试图打开/关闭大写锁定。 I've tried using the virtkey module, but it's not working. 我尝试过使用virtkey模块,但是它没有用。 That module does work for other keys though, so I don't think I'm using the module incorrectly. 该模块确实适用于其他键,因此我认为我没有错误地使用该模块。

Does anybody have a way for doing this? 有人有办法这样做吗?

Just to be clear, I want to actually toggle caps lock on/off and not just the LED. 为了清楚起见,我想实际上打开/关闭大写锁定而不仅仅是LED。

Thanks! 谢谢!

This works for me (turns the led on and off as well as enable/disable caps) 这适用于我(打开和关闭LED以及启用/禁用大写)

import virtkey

v = virtkey.virtkey()
v.press_keycode(66)
v.release_keycode(66)  # first release doesn't release it
v.release_keycode(66)

Here are some more examples 这里有一些例子

v.press_keycode(66)    # turns capslock on
v.release_keycode(66)
v.press_keycode(66)    # turns capslock off
v.release_keycode(66)

You can also get a toggle like behaviour just using release_keycode 您还可以使用release_keycode获得类似切换的行为

v.release_keycode(66)  # toggles capslock
v.release_keycode(66)  # toggles back again capslock

Funny thing. 有趣的事情。 I gave virtkey a try in the interpreter, and it seemed like it was working. 我在解释器中尝试了virtkey,看起来它正在工作。 After calling press_keycode(50) , my machine acts like caps lock has been pressed (Ubuntu 9.10, python-virtkey .50ubuntu2). 在调用press_keycode(50) ,我的机器就像按下了大写锁定(Ubuntu 9.10,python-virtkey .50ubuntu2)。 If I release the key- release_keycode(50) - it acts like nothing has happened. 如果我释放key- release_keycode(50) - 它就像没有发生任何事情一样。

import virtkey

v = virtkey.virtkey()
v.press_keycode(50)

if run as a script, leaves the caps key pressed for a virtual terminal tab. 如果作为脚本运行,则按下大写键以获取虚拟终端选项卡。 Very annoying. 很烦人。

Have you looked into the xvkbd command? 你看过xvkbd命令了吗? It's an X virtual keyboard with command line options, and worst case you could call it from python. 它是一个带有命令行选项的X虚拟键盘,最糟糕的情况是你可以从python中调用它。 Another idea to look into is xmodmap, which lets you re-map keys in X. See this post on SU for an example. 另一个需要考虑的想法是xmodmap,它允许你重新映射X中的键。有关示例,请参阅SU上的这篇文章

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

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