我想制作像 t 这样的键盘快捷键,当主窗口关闭时(但进程正在运行,因为程序有一个统一的应用程序),它会起作用。我看到了一个 package keybinder,但似乎不能将它与 Gtk3 和 pygobject 一起使用。还是可以?那怎么办?如果没有,还有其他方法吗? 该应用程序适用于 linux (ubuntu),我使用 python 2.7。
最佳答案
Keybinder 可以很好地与 python3、Gtk3 和 pygi 配合使用。源代码树中没有工作示例。
#!/usr/bin/env python3
"""
example-gi-py3.py
Looked at a pull request that was built for py2.x, but
overwrote the original py instead of making a separate example.
I wouldn't have accepted that pull request either.
The keybinder.init() part wasn't in the original example.
aking1012.com@gmail.com
public domain
"""
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('Keybinder', '3.0')
from gi.repository import Gtk
from gi.repository import Keybinder
def callback(keystr, user_data):
print ("Handling", user_data)
print ("Event time:", Keybinder.get_current_event_time())
Gtk.main_quit()
if __name__ == '__main__':
keystr = "<Ctrl><Alt>M"
Keybinder.init()
Keybinder.bind(keystr, callback, "keystring %s (user data)" % keystr)
print ("Press", keystr, "to handle keybinding and quit")
Gtk.main()
注意事项: 未经全面测试,但作为一个简单的示例,它似乎有效。
关于python - 如何使用 python(和 Gtk3)制作全局键盘快捷键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16613435/