我的 X System 应用程序保持在后台(并在面板中作为指示器),并且无论何时用户点击一个键都应该弹出,无论是什么活动窗口。类似于菜单应用程序。
尝试了以下方法:
- 在 Linux 上侦听 python 中的全局组合键 但找不到如何集成这个 Gtk 主循环。
- 使用 Python gtk3 在 X 上进行全局键绑定 这个问题的唯一答案是 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()
尽管这捕获了我的输入,但它从不显示窗口。请帮忙 : )