如何获取箭头键,并在Linux中的键盘上输入键,使其像windows7一样

问题描述:

我正在开发一个程序来控制一个仅连接键盘的机器。我使用的是Python 2.7和Tkinter 8.5。我正在使用 OptionMenu 来允许用户在机器上进行设置。

I am developing a program to control a machine that will have only a keypad connected. I am using Python 2.7 and Tkinter 8.5. I am using OptionMenus to allow user to do setup on machine.

当我在Windows下运行时,我可以使用键盘上的箭头键遍历通过下拉列表,然后使用键盘输入选择选项。这在Linux上不起作用(Debian Wheezy)。

When I run under Windows I am able to use arrow keys on keypad to traverse thru drop down list, then use key pad enter to pick option. This is not working on Linux (Debian Wheezy).

如何绑定 KP_Enter 表现为返回键?

How do I bind KP_Enter to behave as return key?

import Tkinter

def c(self, event):
   event.b[".keysym"] = "<<space>>"
   print "button invoked"

t = Tkinter.Tk()

b = Tkinter.OptionMenu(t, ".500", ".510", ".520", 
                       ".550", ".560", ".570", ".580", command=c)
t.bind("<KP_Enter>", c)
e = Tkinter.Entry()
e.pack()
b.pack(anchor=Tkinter.E)

t.mainloop()


使用此脚本(从这里),当您按任意键时,应该很容易识别Tkinter触发的关键事件,无论是返回> < KP_Enter> ,或(以某种方式,也许您的键盘有一个有趣的地图)别的东西。

With this script (from here), it should be easy to identify the key event triggered by Tkinter when you press any key, whether that is <Return>, <KP_Enter>, or (somehow, maybe your keypad has a funny mapping) something else.

当您按所需按钮时,请查看控制台输出,并在实际代码中使用该键事件名称。

Just look at the console output when you press the desired button, and use that key event name in your actual code.

import Tkinter

def callback(e):
    print e.keysym

w = Tkinter.Frame(width=512, height=512)
w.bind("<KeyPress>", callback)
w.focus_set()
w.pack()
w.mainloop()