如何定义自动键快捷方式,将相同的击键发送到具有特定标题的窗口

zub*_*uba 7 gnu-screen autokey

如何定义全局快捷键Ctrl+a 1使用 AutoKey 将相同的击键Ctrl+发送a 1到标题为“gnu screen”的窗口?如果没有这样的标题窗口,它应该显示一个弹出消息“找不到目标窗口”。

use*_*.dz 5

    \n
  1. 安装提供通知发送功能的 libnotify-bin

    \n\n
    sudo apt-get install libnotify-bin\n
    Run Code Online (Sandbox Code Playgroud)
  2. \n
  3. 创建新脚本:

    \n\n
    import time\nimport subprocess\n\n#no need\n#keyboard.release_key("<ctrl>")\n\n# wait_for_keypress does not return any thing to distinguish between target key pressed or timeout reached.\n# So if time is less than the timeout then it was key press.\nstart_time = time.time()\nkeyboard.wait_for_keypress("1", timeOut=1)\nif (time.time()-start_time < 0.9):\n    time.sleep(0.2)\n    window.activate("gnu screen")\n    time.sleep(0.1)\n    active_title = window.get_active_title()\n    # if it doesn\'t get same title, then no window titled as gnu screen\n    # it sends a notify message otherwise send the key sequence.\n    if (active_title == "gnu screen"):\n        keyboard.press_key("<ctrl>")\n        keyboard.send_key("a")\n        keyboard.release_key("<ctrl>")\n        keyboard.send_key("1")\n    else:\n        subprocess.Popen([\'notify-send\', "Couldn\'t find destination window"])\n
    Run Code Online (Sandbox Code Playgroud)
  4. \n
  5. 将其热键设置为:Ctrl+a

  6. \n
\n\n

要触发它:Ctrl+a然后1(<1秒)

\n\n
\n\n

调试

\n\n
    \n
  1. 在单独的终端窗口中启动 X Event Tester

    \n\n
    xev -event keyboard\n
    Run Code Online (Sandbox Code Playgroud)
  2. \n
  3. 检查它的窗口标题,我的显示Event Tester

    \n\n
    $ wmctrl -l\n0x03000012 -1       N/A Desktop \xe2\x80\x94 Plasma\n0x030000c1 -1       N/A Desktop \xe2\x80\x94 Plasma\n0x0300001b -1       N/A Plasma\n0x06a00098  0        PC User User - Ask Ubuntu - Mozilla Firefox\n0x01a00067  0       N/A user : screen\n0x01a000cd  0       N/A user : xev\n0x04600001  0       N/A Event Tester\n0x01e0015d  0        PC AutoKey\n
    Run Code Online (Sandbox Code Playgroud)
  4. \n
  5. 将脚本修改为目标Event Tester窗口。

    \n\n
    import time\nimport subprocess\n\nstart_time = time.time()\nkeyboard.wait_for_keypress("1", timeOut=1)\nif (time.time()-start_time < 0.9):\n    time.sleep(0.2)\n    window.activate("Event Tester")\n    time.sleep(0.1)\n    keyboard.press_key("<ctrl>")\n    keyboard.send_key("a")\n    keyboard.release_key("<ctrl>")\n    keyboard.send_key("1")\n
    Run Code Online (Sandbox Code Playgroud)
  6. \n
  7. 如果xev接收到按键序列。它的输出应该类似于:

    \n\n
    KeyPress event, serial 34, synthetic NO, window 0x4600001,\n    root 0xb2, subw 0x0, time 55057700, (1053,140), root:(945,303),\n    state 0x0, keycode 105 (keysym 0xffe4, Control_R), same_screen YES,\n    XLookupString gives 0 bytes: \n    XmbLookupString gives 0 bytes: \n    XFilterEvent returns: False\n\nKeyPress event, serial 34, synthetic YES, window 0x4600001,\n    root 0xb2, subw 0x0, time 0, (1,1), root:(1,1),\n    state 0x0, keycode 38 (keysym 0x61, a), same_screen YES,\n    XLookupString gives 1 bytes: (61) "a"\n    XmbLookupString gives 1 bytes: (61) "a"\n    XFilterEvent returns: False\n\nKeyRelease event, serial 35, synthetic YES, window 0x4600001,\n    root 0xb2, subw 0x0, time 0, (1,1), root:(1,1),\n    state 0x0, keycode 38 (keysym 0x61, a), same_screen YES,\n    XLookupString gives 1 bytes: (61) "a"\n    XFilterEvent returns: False\n\nKeyRelease event, serial 35, synthetic NO, window 0x4600001,\n    root 0xb2, subw 0x0, time 55057701, (1053,140), root:(945,303),\n    state 0x4, keycode 105 (keysym 0xffe4, Control_R), same_screen YES,\n    XLookupString gives 0 bytes: \n    XFilterEvent returns: False\n\nKeyPress event, serial 35, synthetic YES, window 0x4600001,\n    root 0xb2, subw 0x0, time 0, (1,1), root:(1,1),\n    state 0x0, keycode 10 (keysym 0x31, 1), same_screen YES,\n    XLookupString gives 1 bytes: (31) "1"\n    XmbLookupString gives 1 bytes: (31) "1"\n    XFilterEvent returns: False\n\nKeyRelease event, serial 35, synthetic YES, window 0x4600001,\n    root 0xb2, subw 0x0, time 0, (1,1), root:(1,1),\n    state 0x0, keycode 10 (keysym 0x31, 1), same_screen YES,\n    XLookupString gives 1 bytes: (31) "1"\n    XFilterEvent returns: False\n
    Run Code Online (Sandbox Code Playgroud)
  8. \n
\n\n

请注意,我已经删除了if检查活动标题的条件。该脚本激活/升起 xev 窗口,但检查未找到正确的标题。我刚刚收到通知消息。

\n


Mos*_*zdi 0

我想到的第一件事是将你想做的事情分成两部分:

  1. 编写一个 bash 函数来检查标题为您想要的任何窗口,对您来说它将是“gnu screen”,如果没有找到,则会弹出一条消息“无法找到目标窗口!” 为此,您首先需要安装wmctrl

    sudo apt-get install wmctrl
    
    Run Code Online (Sandbox Code Playgroud)

    它是一个 Windows 控制器,您可以使用它来获取 Windows 信息,包括其 PID、标题、位置、大小、桌面编号等。在您的 中添加以下代码.bash_aliases

    function find_window {
      # Get the list of all windows 
      # and select the line containing a substring given as 
      # an argument to the script
      title=$1
    
      window_found=`wmctrl -l | grep $title | awk '{print $3}'`
    
      # If nothing is found, echo a message
      if [ -z "$window_found" ]; then
        notify-send -i face-crying "Can't find specified window!"
      else
        wmctrl -a $1
      fi
    }
    
    Run Code Online (Sandbox Code Playgroud)

    您可以更改通知的外观。只需查看 的联机帮助页即可notify-send

  2. 使用您的系统设置定义自定义短路。请参阅此处简单添加find_window "gnu screen"作为要运行的命令。您可以根据需要定义键盘序列。

编辑:如果窗口存在,它将切换到该窗口!这就是 theelseif then else作用。