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

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

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

答案1

赤裸裸的肮脏解决方案

要实现一个能完成你想要的操作的命令,其实比乍一看要复杂得多。问题是要同时降低窗口并保持窗口顺序(z 方向),这似乎几乎是不可能的。和都xdotool提供wmctrl命令来增加一扇窗户,但不是降低一个窗口。

以下解决方案是一种肮脏的 hack/变通方法,但无论如何它都能很好地工作并且可靠。它同时使用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]])

如何使用

  1. 安装wmctrlxdotool

    sudo apt-get install xdotool wmctrl
    
  2. 将上述脚本复制到一个空文件中,另存为sendtoback.py

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

    python3 /path/to/sendtoback.py
    

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

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

    python3 /path/to/sendtoback.py
    

    到您选择的快捷键。

笔记

在某些情况下(特别是在较慢的系统上),线路中的睡眠时间:

subprocess.Popen(["xdotool", "mousemove", "--sync", top_x, top_y]); time.sleep(0.3)

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

答案2

Gnomegnome-keybinding-properties在 Gnome 2 中提供了键绑定,gnome-control-center keyboard在 Gnome 3 中也提供了键绑定。

在 Gnome 2 中,接近您想要的默认活动键绑定是

如果窗口被另一个窗口遮住,则升起该窗口,否则降低该窗口

Win使用+的快捷方式space


编辑:我现在正在使用mate,它是 Gnome 2 的一个分支,使用命令行会给我 gnome-keybinding-properties 的“未找到命令”,或者 gnome-control-center 的核心转储。

但使用 UI,可通过System-> Control Center-> Hardware->访问按键绑定Keyboard Shortcuts。最简单的方法是折叠SoundDesktopAccessibility部分,这样就剩下 了Window Management“如果窗户被另一个窗户遮住,则升起窗户,否则降低窗户”位于该部分的中间,并且在 Mate 中被禁用。

相关内容