在X11中捕获另一个按键事件后如何发送按键

问题描述 投票:0回答:1

[我正在尝试在Linux中实现Python脚本以捕获按键a,再次发送按键a(因此它两次发送a)。

通过https://github.com/PeterHo/Linalfred/blob/master/src/globalhotkey.py处的一些代码,我到达了以下位置。

可以很好地捕获按键,但是发送按键事件没有任何作用。我在这里想念什么?

我也查看了问题globally capture, ignore and send keyevents with python xlib, recognize fake input。发布的解决方案甚至似乎都没有收到事件(将print移植到Python 3之后)。

import time

from Xlib import X, protocol
from Xlib.display import Display
from Xlib.ext import record

display = None
root = None

def handler(reply):
    data = reply.data
    while len(data):
        event, data = protocol.rq.EventField(None).parse_binary_value(data, display.display, None, None)
        if event.type == X.KeyPress:
            keycode = event.detail
            print(keycode)
            if keycode == 38:
                window = Display().get_input_focus().focus
                event = protocol.event.KeyPress(
                    time=int(time.time()),
                    root=root,
                    window=window,
                    same_screen=0, child=X.NONE,
                    root_x=0, root_y=0, event_x=0, event_y=0,
                    state=0,
                    detail=keycode
                )
                window.send_event(event, propagate=True)
                event = protocol.event.KeyRelease(
                    time=int(time.time()),
                    root=root,
                    window=window,
                    same_screen=0, child=X.NONE,
                    root_x=0, root_y=0, event_x=0, event_y=0,
                    state=0,
                    detail=keycode
                )
                window.send_event(event, propagate=True)


def main():
    global display, root
    display = Display()
    root = display.screen().root

    ctx = 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,
        }]
    )
    display.record_enable_context(ctx, handler)
    display.record_free_context(ctx)

    while True:
        # Infinite wait, doesn't do anything as no events are grabbed.
        event = root.display.next_event()


if __name__ == '__main__':
    main()
python input keyboard x11 xlib
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.