python-3.x - Python3 中使用 Gtk 和 XLib 的全局热键

标签 python-3.x gtk3 xlib global-hotkey

我的 X 系统应用程序保留在后台(并在面板中作为指示器),并且每当用户按下某个键时就会弹出,无论事件窗口是什么。 类似于菜单应用程序。

尝试了以下方法:

  1. Listening for global key-combinations in python on Linux 但找不到如何集成这个Gtk主循环。
  2. Global keybinding on X using Python gtk3 这个问题的唯一答案是Python 2,并且不起作用。

混合以上两者,我得到以下代码:

from Xlib.display import Display
from Xlib import X, error
from Xlib.ext import record
from Xlib.protocol import rq
from gi.repository import Gtk, GObject, Gdk
import threading


class GlobalKeyBinding(GObject.GObject, threading.Thread):

    def __init__(self):

        GObject.GObject.__init__(self)
        threading.Thread.__init__(self)

        self.setDaemon(True)
        self.display = Display()

        self.Win = Gtk.Window()
        self.Win.add(Gtk.Label("Hello"))

        self.root = self.display.screen().root
        ctx = self.display.record_create_context(
            0,
            [record.AllClients],
            [{
                    'core_requests': (0, 0),
                    'core_replies': (0, 0),
                    'ext_requests': (0, 0, 0, 0),
                    'ext_replies': (0, 0, 0, 0),
                    'delivered_events': (0, 0),
                    'device_events': (X.KeyReleaseMask,
                                    X.ButtonReleaseMask),
                    'errors': (0, 0),
                    'client_started': False,
                    'client_died': False,
            }])
        self.state = 0
        self.display.record_enable_context(ctx, self.handler)
        self.display.record_free_context(ctx)

    def handler(self, reply):
        data = reply.data
        wait_for_release = False
        while len(data):
            event, data = rq.EventField(None).parse_binary_value(
            data, self.display.display, None, None)
            # KEYCODE IS FOUND USERING event.detail
            print(event.detail)

            if event.type == X.KeyPress:
        # BUTTON PRESSED
                print("pressed")
                if not self.state:
                    self.Win.show_all()
                    self.state = 1
                else:
                    self.Win.hide()
                    self.state = 0
            elif event.type == X.KeyRelease:
        # BUTTON RELEASED
                print("released")

    def run(self):
        self.running = True
        while self.running:
            event = self.display.next_event () # registered keycode(or probably rather event) has been received.
            while Gtk.main_iteration():
                Gtk.main_iteration_do(True)


def main():
    print("starting...")
    Gdk.threads_init ()
    keybindings=GlobalKeyBinding()
    keybindings.start ()
    Gtk.main ()

main()

虽然这捕获了我的输入,但它从未显示窗口。 请帮忙:)

最佳答案

所以不需要使用XLib。全局热键可以使用keybinder模块来实现。 示例:

import gi
gi.require_versions({"Gtk": "3.0", "Gdk": "3.0", "Keybinder": "3.0"})
from gi.repository import Gtk, Keybinder


class A:
    def __init__(self):

        self.win = Gtk.Window()
        self.lab = Gtk.Label(label="Hello")

        self.win.add(self.lab)
        self.win.connect("destroy", Gtk.main_quit)
        self.win.show_all()
        # Basic setup of a window with a label

        self.count = 0

        key = Keybinder  # The module to bind keys
        key.bind("<control>m", self.say, "Hello")
        # key.bind(KEYCOMBINATION, FUNC, ARG)
        key.init()  # Call the mainloop something like Gtk.main()

    def say(self, key, msg):
        print(msg)
        self.lab.set_label(f"Pressed {key} {self.count} times")
        self.count += 1


A()  # Call the object
Gtk.main()  # Call the main loop

您可以在这里找到有关此模块的更多信息:https://github.com/engla/keybinder

关于python-3.x - Python3 中使用 Gtk 和 XLib 的全局热键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56517261/

相关文章:

python - 字典列表中的字典

c - 如何使用 g_signal_handlers_block_by_func()

gcc - 如何用gtk3编译gtk2.0程序?

python - 如何修复错误 Xlib.error.DisplayConnectionError : Can't connect to display ":0": b'No protocol specified\n'

python - 捕获键盘不允许改变焦点

python - 局部范围,超出封闭范围

python - Pandas - Dataframe.set_index - 如何保留旧索引列

python - 使用 pytest 参数化类测试

C GTK通过ESC关闭后无法再次打开Dialog

c - 使用 X11 的 Xcomposite 扩展获取被覆盖或不可见或最小化的窗口的屏幕截图