用Python检测Linux中的按键组合?

发布于 2024-07-13 04:13:02 字数 602 浏览 11 评论 0原文

我正在尝试捕获按键操作,以便当按下给定的组合时我会触发一个事件。

我搜索了有关如何入门的提示,我能找到的最简单的代码片段是 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 技术交流群。

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

发布评论

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

评论(5

无戏配角 2024-07-20 04:13:02

如果不显示窗口,Tk 似乎不会得到它。 尝试:

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()

对我有用......

Tk does not seem to get it if you do not display the window. Try:

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()

works for me...

树深时见影 2024-07-20 04:13:02

您正在做的是以“原始”模式读取 /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.

許願樹丅啲祈禱 2024-07-20 04:13:02

或者(非 Python 选项)使用 XBindKeys

Alternatively (a non-Python option) use XBindKeys.

拧巴小姐 2024-07-20 04:13:02

好吧,事实证明,使用 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?

倾城月光淡如水﹏ 2024-07-20 04:13:02

tkinter 'bind' 方法仅在 tkinter 窗口处于活动状态时才有效。

如果您希望绑定适用于所有桌面的击键组合(全局键/鼠标绑定),您可以使用 bindglobal (使用pip install bindglobal安装)。 它的工作原理与标准 tkinter 'bind' 完全相同。

示例代码:

import bindglobal
def callback(e):
    print("CALLBACK event=" + str(e))

bg = bindglobal.BindGlobal()
bg.gbind("<Menu-1>",callback)
bg.start()

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:

import bindglobal
def callback(e):
    print("CALLBACK event=" + str(e))

bg = bindglobal.BindGlobal()
bg.gbind("<Menu-1>",callback)
bg.start()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文