如何降低窗口使其落后于所有其他窗口 - 就像鼠标中键一样

Sco*_*and 5 mouse command-line keyboard shortcut-keys unity

我正在寻找相当于在光标位于窗口顶部标题栏时按下鼠标中间滚轮按钮的键盘。这会降低该窗口,使其位于所有其他窗口之后。

Jac*_*ijm 5

一个无耻的肮脏解决方案

实现一个可以执行您想要的命令的命令比乍一看要复杂得多。问题是同时降低窗口并保持窗口顺序(z 方向),这似乎几乎是不可能的。双方xdotoolwmctrl提供命令来提高一个窗口,而不是降低一个窗口。

下面的解决方案是一个肮脏的黑客/解决方法,但它仍然运行良好且可靠。它同时使用wmctrlxdotool,默认情况下它们不在您的系统上。

尽管该脚本是通过键盘快捷键运行的,但它实际上与您在窗口顶部单击鼠标中键时完全相同。它能做什么:

  • 它查找活动窗口(使用xprop -root
  • 查找窗口是否是“普通”窗口(与例如您的桌面不同,它也wmctrl -lG作为窗口列出)
  • 如果是,它会计算窗口顶部的位置,将鼠标移动到计算出的位置,模拟中键单击并将鼠标移回您离开的位置。

这一切都在瞬间发生,因此您甚至不会注意到鼠标移动和向后移动。你唯一注意到的是窗口被发送到后面,这正是你想要的。

剧本

#!/usr/bin/env python3
import subprocess
import time

# find the frontmost window
active = [l for l in subprocess.check_output(["xprop", "-root"]).decode("utf-8").splitlines() \
          if "_NET_ACTIVE_WINDOW(WINDOW)" in l][0].split("#")[-1].strip()
# convert the window-id from xprop- format to wmctrl- format
w_id = active[:2] + str((10-len(active))*"0")+active[2:]
# if the window is a "normal" window, find the window geometry in wmctrl -lG,
# move the mouse to the top of the window and click the middle button
if "_NET_WM_WINDOW_TYPE_NORMAL" in subprocess.check_output(["xprop", "-id", w_id]).decode("utf-8"):
    match = [l for l in subprocess.check_output(["wmctrl", "-lG"]).decode("utf-8").splitlines() if w_id in l][0].split()[2:6]
    current_mousepos = subprocess.check_output(["xdotool", "getmouselocation"]).decode("utf-8").split()
    coords = ([s.replace("x:", "") for s in current_mousepos if s.startswith("x:")][0],
              [s.replace("y:", "") for s in current_mousepos if s.startswith("y:")][0])
    top_x = str(int(int(match[0])+(int(match[2])/2))); top_y = str(int(match[1]) -10)
    # The time.sleep(0.3) possibly needs to be optimized (longer sleep = safer); the 0.3 works fine on my system when used from a keyboard shortcut (which turns out to make a difference...)
    subprocess.Popen(["xdotool", "mousemove", "--sync", top_x, top_y]); time.sleep(0.3)
    # move the mouse back to its original position
    subprocess.Popen(["xdotool", "click", "2"]); time.sleep(0.05)
    subprocess.Popen(["xdotool", "mousemove", coords[0], coords[1]])
Run Code Online (Sandbox Code Playgroud)

如何使用

  1. 安装wmctrlxdotool

    sudo apt-get install xdotool wmctrl
    
    Run Code Online (Sandbox Code Playgroud)
  2. 将上面的脚本复制到一个空文件中,另存为 sendtoback.py

  3. 通过打开终端测试运行脚本,在其中运行命令:

    python3 /path/to/sendtoback.py
    
    Run Code Online (Sandbox Code Playgroud)

    窗口应该被发送到后台,就像您在中键单击时习惯的那样。

  4. 如果一切正常,请选择:系统设置>“键盘”>“快捷方式”>“自定义快捷方式”。单击“+”并添加命令:

    python3 /path/to/sendtoback.py
    
    Run Code Online (Sandbox Code Playgroud)

    到您选择的快捷键。

笔记

在某些情况下(尤其是在较慢的系统上),睡眠时间在行中:

subprocess.Popen(["xdotool", "mousemove", "--sync", top_x, top_y]); time.sleep(0.3)
Run Code Online (Sandbox Code Playgroud)

需要增加。在更快的系统上,它可以减少。