用Python检测Linux中的按键组合?
我正在尝试捕获按键操作,以便当按下给定的组合时我会触发一个事件。
我搜索了有关如何入门的提示,我能找到的最简单的代码片段是 Python 中的 - 我从 此处。 但是,当我从终端运行它并按一些键时,在“按一个键...”语句之后什么也没有发生。
我是不是傻了? 任何人都可以解释为什么没有任何反应,或者建议在 Linux 上实现此目的的更好方法(任何考虑的语言!)?
import Tkinter as tk
def key(event):
if event.keysym == 'Escape':
root.destroy()
print event.char
root = tk.Tk()
print "Press a key (Escape key to exit):"
root.bind_all('<Key>', key)
# don't show the tk window
root.withdraw()
root.mainloop()
I'm trying to capture key presses so that when a given combination is pressed I trigger an event.
I've searched around for tips on how to get started and the simplest code snippet I can find is in Python - I grabbed the code below for it from here. However, when I run this from a terminal and hit some keys, after the "Press a key..." statement nothing happens.
Am I being stupid? Can anyone explain why nothing happens, or suggest a better way of achieving this on Linux (any language considered!)?
import Tkinter as tk
def key(event):
if event.keysym == 'Escape':
root.destroy()
print event.char
root = tk.Tk()
print "Press a key (Escape key to exit):"
root.bind_all('<Key>', key)
# don't show the tk window
root.withdraw()
root.mainloop()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果不显示窗口,Tk 似乎不会得到它。 尝试:
对我有用......
Tk does not seem to get it if you do not display the window. Try:
works for me...
您正在做的是以“原始”模式读取
/dev/tty
。正常的 Linux 输入是“经过处理的”——退格键和行结束符已经为您处理好了。
要在“原始”模式下读取键盘等设备,您需要直接对 IOCTL 进行 Linux API 调用。
请参阅 http://code.activestate.com/recipes/68397/ 获取一些指导关于这一点。 是的,配方在 tcl 中,但它为您提供了如何继续操作的提示。
What you're doing is reading
/dev/tty
in "raw" mode.Normal Linux input is "cooked" -- backspaces and line endings have been handled for you.
To read a device like your keyboard in "raw" mode, you need to make direct Linux API calls to IOCTL.
Look at http://code.activestate.com/recipes/68397/ for some guidance on this. Yes, the recipe is in tcl, but it gives you a hint as to how to proceed.
或者(非 Python 选项)使用 XBindKeys。
Alternatively (a non-Python option) use XBindKeys.
好吧,事实证明,使用 GNOME 时有一个更简单的答案,它根本不涉及任何编程......
http://www.captain.at/howto-gnome-custom-hotkey-keyboard-shortcut.php
存档在 Wayback
只需创建脚本/可执行文件由组合键触发,并将您在 gconf-editor 中创建的“keybinding_commands”条目指向它。
为什么我没有早点想到这一点?
Well, turns out there is a much simpler answer when using GNOME which doesn't involve any programming at all...
http://www.captain.at/howto-gnome-custom-hotkey-keyboard-shortcut.php
Archived on Wayback
Just create the script/executable to be triggered by the key combination and point the 'keybinding_commands' entry you create in gconf-editor at it.
Why didn't I think of that earlier?
tkinter 'bind' 方法仅在 tkinter 窗口处于活动状态时才有效。
如果您希望绑定适用于所有桌面的击键组合(全局键/鼠标绑定),您可以使用 bindglobal (使用
pip install bindglobal
安装)。 它的工作原理与标准 tkinter 'bind' 完全相同。示例代码:
tkinter 'bind' method only works when tkinter window is active.
If you want binding keystrokes combinations that works in all desktop (global key/mouse binding) you can use bindglobal (install with
pip install bindglobal
). It works exactly like standard tkinter 'bind'.Example code: