使用python将按键立即打印到屏幕上 [英] printing key presses to the screen instantly with python

查看:494
本文介绍了使用python将按键立即打印到屏幕上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我希望在Linux上使用python.

我正在尝试制作一种类似于devorak的新键盘布局,但是根据您是否握住热键(热键可能应该是ctrl?),该布局被设置为layout1或layout2.

例如按d->"z"使用键layout1打印到屏幕上

例如按下ctrl d->"x"使用按键layout2打印到屏幕上

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

如果有人(按此顺序)按了(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并按键,则无法区分大小写)

解决方案

如果可以依赖X窗口系统,则可以使用 python-xlib 模块或 xpyb 模块进行访问X窗口系统,并使用 XGrabKey 调用来获取与键盘相关的事件.在每个KeyPress事件上,您都可以打印所按下的键.

现在,如果您真的想编写一个键盘映射,则这完全取决于OS/窗口系统.如果您使用X窗口系统(Ubuntu确实如此),则需要查看X文档中有关如何编写新键盘映射的信息.在Ubuntu上,当前的键盘映射定义应该在/usr/share/X11/xkb中.看一看,然后尝试复制和编辑一个.您可以使用setxkbmap更改当前的键盘映射.

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)

解决方案

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.

这篇关于使用python将按键立即打印到屏幕上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆