4

对于这样一个基本问题,我很惊讶我无法通过搜索找到任何东西......

无论如何,我用 Python 制作了一个 curses 应用程序,可以帮助解决某个 DSiWare 游戏的谜题。有了它,你可以拿一个拼图并单独检查它的组件。按键qweasdzx用于绘制瓷砖(按键排列在某种调色板中)。在按住 Shift 键的同时按下其中一个键会突出显示具有该颜色的图块。我不能要求更自然的控制方案。

所以很遗憾,Shift 再次给我带来了问题(上次我遇到 Shift 问题时,我获得了 Tumbleweed 徽章)。虽然这一次,问题或多或少是大写锁定,它通过反转功能完全搞砸了我的程序。

如何使用 curses 检测 Python 中 Caps Lock 的状态?

编辑:如果您要建议使用单独的模块,我可能应该提醒您 curses - 因此我的程序 - 在 UNIX 领域。

4

2 回答 2

7

I found a solution on my own:

Since curses is completely unaware of the Caps Lock setting according to ΤΖΩΤΖΙΟΥ, I tried an alternative solution. Specifically, I looked up how to check Caps Lock in a BASH script. What I found was this:

Linux only. Requires X Window System.

$ xset q | grep LED
>  auto repeat:  on    key click percent:  0    LED mask:  00000000

The last 0 in that output (the 66th character in the string) is the Caps Lock flag. 1 if it's on, 0 if it's off.

Python can run UNIX system commands with the Linux-only commands module. commands does not appear to interfere with curses.

>>> import commands
>>> # Caps Lock is off.
>>> commands.getoutput("xset q | grep LED")[65]
'0'
>>> # Setting Caps Lock on now.
>>> commands.getoutput("xset q | grep LED")[65]
'1'

This works fine for me; this is a personal-use script, and it's not like my program wasn't already Linux-exclusive. But I do hope somebody has another, more Windows-compatible solution.

I'm going to accept this self-answer for now, but if somebody else can come up with a better working solution, I'd like to see it.

于 2010-07-09T00:40:37.787 回答
3
于 2010-07-08T23:33:00.370 回答