检测 Python 诅咒中的大写锁定

发布于 2024-09-09 08:34:35 字数 416 浏览 1 评论 0原文

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

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

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

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

编辑:如果您建议使用单独的模块,我可能应该提醒您,curses - 以及我的程序 - 属于 UNIX 领域。

For such a basic question, I'm surprised I couldn't find anything by searching...

Anyways, I made a curses app in Python that assists in solving puzzles of a certain DSiWare game. With it, you can take a puzzle and inspect the components of it individually. The keys qweasdzx are used to paint tiles (the keys are arranged in some sort of palette). Pressing one of these keys while holding Shift highlights tiles with that color. I couldn't ask for a more natural control scheme.

So it's a shame that, once again, Shift is giving me issues (last time I had issues with Shift, I earned a Tumbleweed badge). Though this time, the problem is more or less Caps Lock, which completely screws up my program by reversing the functions.

How can I detect the state of Caps Lock in Python with curses?

EDIT: If you're going to suggest using a separate module, I probably should remind you that curses - and therefore my program - is in UNIX territory.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天邊彩虹 2024-09-16 08:34:35

我自己找到了一个解决方案:

由于curses完全不知道根据ΤΖΩΤΖIΟΥ的大写锁定设置,我尝试了另一种解决方案。具体来说,我查找了如何在 BASH 脚本中检查 Caps Lock。我发现的是:

仅限 Linux。需要 X Window 系统。

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

该输出中的最后一个 0(字符串中的第 66 个字符)是 Caps Lock 标志。如果打开则为 1,如果关闭则为 0。

Python 可以使用仅限 Linux 的 命令模块。命令似乎不会干扰诅咒。

>>> 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'

这对我来说效果很好;这是一个个人使用的脚本,而且我的程序并不是 Linux 独有的。但我确实希望有人有另一个与 Windows 更兼容的解决方案。

我现在将接受这个自我回答,但如果其他人可以提出更好的工作解决方案,我希望看到它。

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.

梦行七里 2024-09-16 08:34:35

简短的回答:你不能。

更长的答案:

curses 被创建为基于 terminfo 的库,以简化基于字符的 UI 的创建,而与所使用的终端无关(用于“vt220”、“wyse100”中的终端,...)。

这些终端通过串行线连接,并且与主机之间的通信通过纯文本(由用户输入或由主机输出)或特殊序列(“转义”序列;如果使用特殊键,则由用户输入)按下,如 Prev,或者如果请求光标定位或屏幕清除等特殊操作,则由主机输出)。

我不知道任何哑终端会在按下大写锁定键时发送特殊序列,或者主机查询大写锁定状态;锁定大写字母是终端工作的一部分,主机不需要任何知识。这与当单独按下 Control 键时无法让诅咒程序执行操作的原因类似。

关于curses的一切都与terminfo功能相关;没有任何与您要求的相关的功能。

The short answer: you can't.

A longer answer:

curses was created as a terminfo-based library to ease the creation of character-based UIs independent of the terminal used (for terminal in 'vt220', 'wyse100', …).

These terminals connected through a serial line and the communication to-and-fro the host were through either plain text (input by the user or output by the host) or special sequences ("escape" sequences; input by the user if special keys were pressed, like or Prev, or output by the host if special operations like cursor positioning or screen clearing was requested).

I have no knowledge of any dumb terminal sending a special sequence whenever the Caps Lock key was depressed, or the host querying for the Caps Lock status; locking capitals was part of the job of the terminal, and the host had no need for any knowledge. This is similar to the reason you can't have a curses program act when the Control key is depressed on its own.

Everything about curses relates to terminfo capabilities; there isn't any related capability for what you ask.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文