如何使用 at-spi2 将 Ctrl-l 键输入发送到 GNOME 文件选择器对话框?

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

我想在 Linux 上控制 chromium 文件选择器对话框,而无需任何人为干预。

我正在尝试使用 at-spi2 和 python 来实现它。 (由 gobject-introection 制作的 at-spi2 python 绑定)

我想显示一个文本框,直接使用 Ctrl-L 输入文件路径。
但我不知道如何将 Ctrl-L 发送到文件选择器对话框。


编辑

我以为它只发生在 Chromium 中,但实际上它不仅发生在 chromium 中,还发生在所有其他如 zenity 中。


现在我知道该对话框不属于chromium,实际上它是由xdg-desktop-portal-gnome控制的。

我该怎么做?

我的环境是ArchLinux和Gnome 45,Chromium 122.0.6261.94(官方版本)

以下代码是我尝试使用generate_keyboard_event发送键盘输入,但没有发生任何事情...

def search_chrome_filechooser():
    if not Atspi.is_initialized():
        err = Atspi.init()
        if err != 0:
            print("Something Error:", err)
            return

    root = Atspi.get_desktop(0)
    app = search_widget(root, "xdg-desktop-portal-gnome", "application")
    if app == None:
        print("app not found")
        return
    dialog = search_widget(app, role_name="dialog")
    if dialog is None:
        print("file chooser dialog not found")
        if Atspi.is_initialized():
            Atspi.exit()
        return


    Atspi.generate_keyboard_event(29, None, Atspi.KeySynthType.PRESS)
    Atspi.generate_keyboard_event(38, None, Atspi.KeySynthType.PRESSRELEASE)
    if Atspi.is_initialized():
        Atspi.exit()


def search_widget(root, name: str|None=None, role_name:str|None=None):
    widget = root
    if widget == None:
        return None
    if name == None and role_name == None:
        return
    widget_name = widget.get_name()
    widget_role_name = widget.get_role_name()
    if name == widget_name and role_name == None:
        return widget
    elif name == None and role_name == widget_role_name:
        return widget
    elif name == widget_name and role_name == widget_role_name:
        return widget
    num = widget.get_child_count()
    for v in range(num):
        child = widget.get_child_at_index(v)
        if (result := search_widget(child, name, role_name)) != None:
            return result

search_chrome_filechooser()
linux gtk accessibility gnome
1个回答
0
投票

这是文件选择器获得焦点时起作用的脚本,我使用的是 Ubuntu/Wayland:

from pyatspi import Atspi

def send_ctrl_l():
    if not Atspi.is_initialized():
        Atspi.init()

    # Ensure these key codes are correct for your system
    ctrl_key_code = 29  # Key code for 'Ctrl'
    l_key_code = 38     # Key code for 'L'
    
    # Press 'Ctrl'
    Atspi.generate_keyboard_event(ctrl_key_code, None, Atspi.KeySynthType.PRESS)

    # Press and release 'L'
    Atspi.generate_keyboard_event(l_key_code, None, Atspi.KeySynthType.PRESSRELEASE)

    # Release 'Ctrl'
    Atspi.generate_keyboard_event(ctrl_key_code, None, Atspi.KeySynthType.RELEASE)

    if Atspi.is_initialized():
        Atspi.exit()
    
send_ctrl_l()

我也没有设法将焦点移至对话框。

© www.soinside.com 2019 - 2024. All rights reserved.