【问题标题】:How to send a shortcut by bash to a program?如何通过 bash 向程序发送快捷方式?
【发布时间】:2020-10-03 11:55:59
【问题描述】:

我喜欢向 Firefox 发送刷新标签的快捷方式。

我有什么:

  • 我有一个带有工作终端、Firefox 并安装了 xdotool 的 Linux

我发现/尝试了什么:

我在终端上尝试的内容(没有收到错误消息,它看起来很适合我,就像我没有真正重新加载网页一样)。可以将终端的输出发送到终端而不是火狐。:

xdotool key F5
xdotool key Ctrl + R

问题:

  • 如何通过终端在活动工作区上的活动 Firefox 上的活动选项卡上执行此操作?
  • 如何在工作区 2 上运行终端以在工作区 1 上的活动 Firefox 上运行活动选项卡?

备注和新知识:

  • 现在我找到了以下内容,它能够从 Debian 上的 Firefox 浏览器重新加载活动标签(不知道它也适用于其他浏览器)

  • 不知道这次它可以在一个或多个工作区上的多个浏览器上运行,以及如何将其用于一个特定的工作区,例如在终端上运行它或在工作区 2 上运行 bash 并将其用于一个或多个 Firefox在工作区一上。

    xdotool search --onlyvisible --classname Navigator windowactivate --sync key F5

  • 现在我稍微更改了 MarcoLucidi 的代码。现在它重新加载一个活动选项卡(可以在工作区 1 的活动浏览器上)。我明天稍微测试一下。请参阅以下内容:

    xdotool key --window "$(xdotool search --classname Navigator | head -1)" F5

【问题讨论】:

  • xdotool getactivewindow key F5?
  • xdotool key --window "$(xdotool search --class Firefox | head -1)" F5?
  • @Alfred.37 所以它不起作用?如果将F5 替换为Ctrl+q 会怎样,它会关闭Firefox 窗口吗?
  • 我可以通过键盘按 F5 刷新 FF,而不是通过终端。而且我可以通过键盘 a 而不是终端通过 Ctrl + q 关闭 FF。这对我来说听起来不错,我仍然没有将 xdotool 的终端输出发送到特定程序。
  • fwiw,在“xdotool 示例浏览器”上运行谷歌搜索并收到了几个点击,例如,thisthis

标签: linux bash firefox keyboard-shortcuts shortcut


【解决方案1】:

通过 bash 将 F5 发送到浏览器的示例,用于重新加载页面:

xdotool sleep 0.5 search --onlyvisible --classname Navigator windowactivate --sync key F5
xdotool sleep 0.5 search --onlyvisible --classname Navigator windowactivate --sync keyup F5

通过 bash 发送 Ctrl+F5 到浏览器的示例,用于重新加载缓存和页面:

xdotool sleep 0.5 search --onlyvisible --classname Navigator windowactivate --sync key Ctrl+F5
xdotool sleep 0.5 search --onlyvisible --classname Navigator windowactivate --sync keyup Ctrl
xdotool sleep 0.5 search --onlyvisible --classname Navigator windowactivate --sync keyup F5

备注: keyup 稍微修复了有问题的 xdotool。

【讨论】:

    【解决方案2】:

    现在在错误跟踪器上发现了错误:

    发现如下bug原因:

    “xdotool 似乎不起作用的事实可能与应用程序检测和丢弃合成事件有关:

    Sending keystrokes to a specific window uses a different API than simply typing to the active window.
    
    [...]
    
    Many programs observe this flag and reject these events."
    

    来源:Automatic web-page refresh using xdotool - not sending key after window focus

    找到以下解决方案:

    “考虑到这一点,我能够使用下面的一系列命令使其工作。这会重新加载 Chromium 和 Firefox。

    cwid=$(xdotool getwindowfocus) # Save the current window
    twid=$(xdotool search --name somename)
    xdotool windowactivate $twid
    sleep 0.1 # The key event might be sent before the window has been fully activated
    xdotool key --window $twid F5
    xdotool windowactivate $cwid # Done, now go back to where we were
    

    " 来源:Automatic web-page refresh using xdotool - not sending key after window focus

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-04
      • 2011-09-02
      • 2020-03-07
      • 2019-12-16
      • 2022-10-25
      • 1970-01-01
      相关资源
      最近更新 更多