使用python立即将按键打印到屏幕上

发布于 2024-11-09 19:00:39 字数 1391 浏览 4 评论 0原文

我知道这个问题很长,但我真正想知道的是粗体的。

我更喜欢在 Linux 上使用 python。

我正在尝试制作一个类似于 devorak 的新键盘布局,但布局设置为布局 1 或布局 2,具体取决于您是否按住热键(热键可能应该是 ctrl?),

例如按 d -> ; 打印到屏幕上

“z”使用按键布局 1例如 按 ctrl d -> “x”使用按键布局打印到屏幕2

我的主要问题(也是需要回答的问题)是字符需要打印到屏幕的方式。

如果有人按下按键(按此顺序)“(a)(b)(c)(d)(ctrl+d)(shift+e=E)(f)(Enter)”

现在让我们说输出对于这些按键应该是“oijzxKb” 我不希望输出以新行呈现:

o
i
j
z
x
K
b

我希望字符在按下每个字符时立即出现在屏幕上(无需等待他们按 Enter 键)。

e.g.
press o

Screenshot1 o

press i

Screenshot2 oi

press j

Screenshot3 oij

.. etc

我认为我需要以下:

  • 一种立即读取按键的方法
  • 一种立即打印按键的方法(到终端或 GUI 或最初最简单的任何东西,如果它适用于任何编辑器,那就太酷了!)

我可能可以在 PyGame 中执行此操作(但是然后我可能无法剪切和粘贴等)并且我想应该有一个更简单的方法。

我正在使用 Logitech G110 键盘,我最终可能希望在所有设备上的所有应用程序上使用它来替代我的 qwerty 键盘。

谢谢!

编辑:解决方案:

感谢第一个回复, 使用来自 http://code.activestate.com/recipes/134892/ 的 Getch

getch = _Getch()
word=""
while True:
    c=getch.impl()
    if c=="a":
        word+="z"
    elif ord(c)==127: #backspace
        word=word[:-1]
    else:
        word+=c
    print word

这就足够了现在谢谢你。一旦我对改进感到满意,我就会考虑做一些较低级别的、特定于操作系统的事情,而不需要 python。

getch 的一个问题是 ctrl+a 无法区分 ctrl+A (例如,如果按住 ctrl 并按键,它无法区分大小写)

I know this question is long but what I really want to know is in bold.

I would prefer to use python on Linux.

I'm trying to make a new keyboard layout kind of like devorak but the layout is set to either layout1 or layout2 depending on if you are holding a hot key or not (the hot key should probably be ctrl?)

e.g. press d -> "z" prints to the screen using key layout1

e.g. press ctrl d -> "x" prints to the screen using key layout2

My main problem (and question that needs answering) is the way characters need to print to the screen.

if someone presses the keys (in this order) "(a)(b)(c)(d)(ctrl+d)(shift+e=E)(f)(Enter)"

now lets say the output for these key presses should be "oijzxKb"
I don't want output to render with new lines:

o
i
j
z
x
K
b

I want the characters to appear instantly on the screen as each character is pressed (without waiting for them to press enter).

e.g.
press o

Screenshot1 o

press i

Screenshot2 oi

press j

Screenshot3 oij

.. etc

I assume I will need the following:

  • a way to read keypresses instantly
  • a way to print key presses instantly (to the terminal or a GUI or whatever is easiest initially, if it worked on any editor that would be cool!)

I could probably do this in PyGame (but then I probably wouldn't be able to cut and paste etc) and I'm guessing there should be an easier way.

I'm using a Logitech G110 keyboard, I may eventually want to use this as an alternative to my qwerty keyboard on all my applications across all my devices.

Thanks!

EDIT: SOLUTION:

Thanks to the first response,
using Getch from http://code.activestate.com/recipes/134892/

getch = _Getch()
word=""
while True:
    c=getch.impl()
    if c=="a":
        word+="z"
    elif ord(c)==127: #backspace
        word=word[:-1]
    else:
        word+=c
    print word

This will suffice for now thank you. Once I'm happy with refinement I'll look at doing something lower level, operating system specific without python.

One problem with getch however is that ctrl+a cant be distinguished between ctrl+A (e.g. if you hold ctrl and press keys, it can't tell the difference between upper and lower case)

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

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

发布评论

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

评论(2

一桥轻雨一伞开 2024-11-16 19:00:39

如果可以依赖X窗口系统,您可以使用 python-xlib 模块或 < a href="http://cgit.freedesktop.org/xcb/xpyb/" rel="nofollow">xpyb 模块访问 X 窗口系统并使用 XGrabKey 调用以获取键盘相关事件。在每次 KeyPress 事件发生时,您将能够打印按下的键。

现在,如果您确实想编写键盘映射,这完全取决于操作系统/窗口系统。如果您使用X窗口系统(Ubuntu使用),您需要检查X文档以了解如何编写新的键盘映射。在 Ubuntu 上,当前的键盘映射定义应位于 /usr/share/X11/xkb 中。看一看,然后尝试复制并编辑一个。然后您可以使用 setxkbmap 更改当前的键盘映射。

If it's ok to depends on the X window system, you can use the python-xlib module or the xpyb module to access the X window system and use a XGrabKey call to grab the keyboard related events. Upon each KeyPress event you will be able to print the pressed key.

Now, if you really want to write a keymap, this is totally OS/window system dependent. If you use the X window system (Ubuntu does), you need to check the X documentation about how to write a new keymap. On Ubuntu, the current keymaps definition should be in /usr/share/X11/xkb. Take a look, and try to copy and edit one. You can use setxkbmap to change the current keymap then.

缘字诀 2024-11-16 19:00:39

要修改键盘的键映射,您必须使用操作系统提供的工具。出于安全原因,大多数应用程序不接受生成的事件。

在您的情况下,这将是 xmodmap。不要忘记使用 -pke 选项创建当前键盘映射的备份,因为您会犯错误 - 然后您的键盘将不再工作。

如果您还希望新的键盘映射在控制台上工作,请查看kbd,它在内核级别更改键盘布局。

To modify the key mapping of your keyboard, you must use the tools provided by your OS. Most applications don't accept generated events for security reasons.

In your case, that would be xmodmap. Don't forget to create a backup of your current keymap using the -pke option because you will make a mistake - and then, your keyboard won't be working anymore.

If you also want your new keymap work on the console, have a look at the kbd package which changes the keyboard layout at the kernel level.

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