Gnome Keybinder 没有正确绑定到 XF86AudioPlay

Gnome Keybinder doesn't bind properly to XF86AudioPlay

我已经成功地获得了一个 Python 程序来监听 Control+Alt+PControl+Alt+O 即使 window 没有焦点。

然而,即使成功绑定(如果拼写正确),我似乎也无法捕获 <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 : 
'''
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,而我的 Ubuntu 16.04 与 Unity 界面似乎没问题:


回复评论

这里是 xev 输出:

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+Alt+PControl+Alt+O 继续工作。

这是我添加/更改的顺序:

    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 成功,但第二个 keybinder 打印到终端:

Keybinder.bind() failed 2.

Question: 'Keybinder' doesn't bind properly to '<XF86AudioPlay>'

  1. 根据文档,人必须在任何其他 Keyboard.... 函数之前调用 Keyboard.init()

  2. 通过评估 Keyboard.bind(... 函数的 return 值,人可以验证绑定是否成功。

    keysym = '<XF86AudioPlay>'
    if not Keybinder.bind(keysym, callback):
        print('bind "{}" failed!'.format(keysym))
    
  3. 根据 xevGNOME gettings 的输出,keysym 必须是 'XF86AudioPlay' 且不带括号。
    Keybinder.bind('XF86AudioPlay', callback)
    

参考:

  • gtk-keybinder-does-not-respond
  • Keybinder.init

    Initialize the keybinder library. This function must be called after initializing GTK, before calling any other function in the library. Can only be called once.

  • Keybinder.bind

    Returns: True if the accelerator could be grabbed
    Grab a key combination globally and register a callback to be called each time the key combination is pressed.


使用 Python 测试:3.5 - gi.__version__:3.22.0 - keybinder-3.0、0.3.2-1