python 键盘输入立刻反应_使用python将按键立即打印到屏幕上

1586010002-jmsa.png

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值