在 Unity 中,按Super+1可9启动或将焦点切换到应用程序。我可以在 Lubuntu(即 LXDE)中分配具有类似属性的键盘快捷键吗?
更准确地说,我可以设置一个快捷方式,这样如果应用程序已经打开,焦点就会切换到它,工作区也会相应改变(就像在 Unity 中一样)吗?到目前为止,我只设法分配了一个启动应用程序(新实例)的快捷方式。
答案1
Openbox(LXDE 的窗口管理器)没有内置此功能,但可以通过工具我们可以编写一个脚本来复制甚至增强 Unity 的行为。
首先,确保您已经安装了 xdotool:
sudo apt-get install xdotool
将以下脚本保存为focus_or_launch
并使其可执行:
#!/bin/bash
# NAME: focus_or_launch
# VERSION: 0.4
# AUTHOR: (c) 2014 Glutanimate <https://github.com/Glutanimate/>
#
# DESCRIPTION: Focus existing app window or launch application if no
# window found
#
# Simplified version of a script by Henning Bekel
# (https://bbs.archlinux.org/viewtopic.php?pid=625009#p625009)
#
# DEPENDENCIES: xdotool
#
# LICENSE: GNU GPLv3 (http://www.gnu.de/documents/gpl-3.0.en.html)
#
# NOTICE: THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
# EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
# PROVIDE THE PROGRAM “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR
# IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND
# PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE,
# YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
#
# IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY
# COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS
# PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL,
# INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
# THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED
# INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE
# PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER
# PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
#
# USAGE: focus_or_launch <command>
# EXAMPLE: focus_or_launch google-chrome
############# GLOBVAR/PREP ###############
Executable="$1"
ExecutableBase="$(basename "$Executable")"
Usage="\
Usage: $(basename $0) command
E.g.: $(basename $0) google-chrome\
"
############## USGCHECKS #################
if [[ $# -ne 1 || "$1" =~ ^(-h|--help)$ ]]; then
echo "$Usage"
exit 1
fi
################ MAIN ####################
MostRecentWID="$(xdotool search --class --name "$ExecutableBase" | tail -1 2> /dev/null)"
if [[ -z "$MostRecentWID" ]]; then
echo "$ExecutableBase not found. Launching new window."
"$Executable" > /dev/null 2>&1 &
disown
else
echo "Focusing existing instance of $ExecutableBase."
# use brute-force approach if activating most recent WID doesn't work
xdotool windowactivate "$MostRecentWID" 2>&1 | grep failed \
&& xdotool search --class --name "$ExecutableBase" windowactivate %@
fi
脚本的工作原理如下:运行时focus_or_launch application
xdotool 将查找应用程序的现有实例并聚焦其中一个。如果未找到任何实例,脚本将启动一个新实例。
要将此脚本与您的桌面会话集成,请编辑您的 openbox 配置文件(例如~/.config/openbox/lxde-rc.xml
,../lubuntu-rc.xml
或../rc.xml
),并为您想要控制的每个应用程序添加类似于以下内容的部分:
<keybind key="W-g">
<action name="Execute">
<command>focus_or_launch gedit</command>
</action>
</keybind>
确保将其替换focus_or_launch
为系统上脚本的路径,或者将脚本复制到您的$PATH
。
如果您不想让脚本启动新的应用程序实例,只需删除以下部分:
"$Executable" > /dev/null 2>&1 &
disown
PS:该脚本应适用于所有与 xdotool 兼容的桌面环境(例如 Unity、GNOME、XFCE……)