我已经成功地获得了一个Python程序来监听Control + Alt + P和Control + Alt + O即使窗口没有焦点。
但是即使成功绑定(如果拼写正确),我似乎也无法捕获<FX86Audio...>
事件。
这是一个有效的代码段:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
##!/usr/bin/python3.5 <- Test this every now and then for compatability
# Reqquires:
# sudo apt-get install libkeybinder-3.0-0 gir1.2-keybinder
'''
From : https://stackoverflow.com/questions/56517261/global-hotkey-in-python3-using-gtk-and-xlib
'''
import gi
# Python 2
gi.require_version('Gtk', '3.0')
gi.require_version('Gdk', '3.0')
gi.require_version('Keybinder', '3.0')
# Python 3
#gi.require_versions({"Gtk": "3.0", "Gdk": "3.0", "Keybinder": "3.0"})
from gi.repository import Gtk, Gdk, Keybinder, Pango
class A:
def __init__(self):
# Basic setup of a window with a label
self.win = Gtk.Window()
self.lab = Gtk.Label(label="Hello World!")
self.lab.modify_font(Pango.FontDescription("sans 36"))
self.win.add(self.lab)
self.win.connect("destroy", Gtk.main_quit)
self.win.show_all()
self.count = 0
key = Keybinder # The module to bind keys
# key.bind(KEYCOMBINATION, FUNC, ARG)
key.bind("<control><alt>p", self.say, "Hello")
key.bind("<control><alt>o", self.say, "World!")
key.bind("<XF86AudioPlay>", self.say, "World :)")
key.bind("<XF86AudioPause>", self.say, "World :(")
key.bind("<AudioPlay>", self.say, "World :/")
key.init() # Call the mainloop something like Gtk.main()
def say(self, key, msg):
self.count += 1
print(msg)
# Python 2
text="Pressed "+ key + " " + str(self.count) + " times"
self.lab.set_label(text)
# Python 3
# self.lab.set_label(f"Pressed {key} {self.count} times")
A() # Call the object
if not Keybinder.bind("<control><alt>p", A.say, "Bad News 1"):
print "Keybinder.bind() failed 1."
if not Keybinder.bind("<XF86AudioPlay>", A.say, "Bad News 2"):
print "Keybinder.bind() failed 2."
Gtk.main() # Call the main loop
如前所述,这些键绑定是可接受的,但不起作用:
key.bind("<XF86AudioPlay>", self.say, "World :)")
key.bind("<XF86AudioPause>", self.say, "World :(")
key.bind("<AudioPlay>", self.say, "World :/")
键盘可用于暂停和弹奏rhythembox
。
认为Unity妨碍了我将<XF86AudioPlay>
键盘快捷键重新映射到另一个序列,但无济于事。
[在其他网站上,它讨论的是gsettings
,对于具有Unity接口的Ubuntu 16.04,我的似乎还可以:]
这里是xev
输出:
+ Alt + P和Control + Alt + O继续起作用。KeymapNotify event, serial 37, synthetic NO, window 0x0, keys: 13 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 16 0 0 0 0 0 0 0 0 0 0 KeyRelease event, serial 37, synthetic NO, window 0x3a00001, root 0x259, subw 0x0, time 802402347, (1900,975), root:(3820,1027), state 0x10, keycode 172 (keysym 0x1008ff14, XF86AudioPlay), same_screen YES, XLookupString gives 0 bytes: XFilterEvent returns: False
添加
init
和更改顺序不会使XF86AudioPlay
起作用,但另一方面却没有破坏Control
这是我添加/更改的顺序:
Keybinder.init() key = Keybinder # The module to bind keys key.init() # Call the mainloop something like Gtk.main()
回复评论2
添加:
if not Keybinder.bind("<control><alt>p", A.say, "Bad News 1"): print "Keybinder.bind() failed 1." if not Keybinder.bind("<XF86AudioPlay>", A.say, "Bad News 2"): print "Keybinder.bind() failed 2."
第一个键盘绑定程序成功,但是第二个键盘打印到终端:
Keybinder.bind() failed 2.
即使窗口没有焦点,我也已经成功地获得了一个Python程序来监听Control + Alt + P和Control + Alt + O。但是我似乎无法捕获
问题
:'Keybinder'未正确绑定到'<XF86AudioPlay>'