我想要做如何将键盘上的一个键映射到另一个键?但对于不同的程序有不同的映射。
具体来说,每当特定窗口获得焦点时,我希望将 F6 键映射到 F7。
如果有什么区别的话,我会使用 xUbuntu 14.04。
答案1
使用 xdotool 的解决方案
我不认为有任何 DE 有内置的选项,但在xdotool
我们可以编写一个自定义脚本来实现这个效果:
#!/bin/bash
# 'f6f7swap.sh' by Glutanimate
#
# Reassign F6 to F7 if specific application has focus
# Dependencies: xdotool x11-utils
#
# Usage: f6f7swap.sh '<full window class>'
# Example: f6f7swap.sh '"gnome-terminal", "Gnome-terminal"'
# --Variables--
WinClass="$1"
# --Main--
# get currently active window ID
ActiveWinID="$(xdotool getactivewindow)"
# get window class of active window
ActiveWmClass="$(xprop -id "$ActiveWinID" \
| grep "WM_CLASS" \
| awk -F' = ' '{print $2}' )"
# Compare with provided window class
if [[ "$ActiveWmClass" = "$WinClass" ]]; then
echo "Target window found. Sending 'F7'"
xdotool key F7
else
echo "Target window not found. Sending 'F6'"
xdotool key F6
fi
指示
- 确保您已
xdotool
安装xprop
(sudo apt-get install xdotool x11-utils
) - 将此脚本另存为
f6f7swap.sh
并使其可执行 xprop | grep WM_CLASS
通过运行并单击示例窗口来确定目标窗口/应用程序的窗口类f6f7swap.sh
使用完整窗口类作为参数运行,例如:f6f7swap.sh '"gnome-terminal", "Gnome-terminal"'
f6f7swap.sh
将尝试将活动窗口与提供的窗口类进行匹配。如果匹配,脚本将发送“F7”;如果不匹配,脚本将发送“F6”。- 将完整命令(例如
f6f7swap.sh '"gnome-terminal", "Gnome-terminal"'
)绑定到F6键盘偏好设置中
更新脚本
以下是该脚本的更通用版本,允许交换您选择的任何键:
#!/bin/bash
# 'keyswap.sh' by Glutanimate
#
# Reassign keys if specific application has focus
# Dependencies: xdotool
#
# Usage: keyswap.sh '<full window class>' '<input key>' '<output key>'
# Example: keyswap.sh '"gnome-terminal", "Gnome-terminal"' 'F6' 'F7'
# --Variables and Checks--
Usage="Usage: keyswap.sh '<full window class>' '<input key>' '<output key>'"
if [[ "$#" != "3" ]]; then
echo "Error: Insufficient arguments"
echo "$Usage"
exit 1
fi
WinClass="$1"
OrigKey="$2"
ReplKey="$3"
# --Main--
# get currently active window ID
ActiveWinID="$(xdotool getactivewindow)"
# get window class of active window
ActiveWmClass="$(xprop -id "$ActiveWinID" \
| grep "WM_CLASS" \
| awk -F' = ' '{print $2}' )"
# Compare with provided window class
if [[ "$ActiveWmClass" = "$WinClass" ]]; then
echo "Target window found. Sending $ReplKey"
xdotool key "$ReplKey"
else
echo "Target window not found. Sending $OrigKey"
xdotool key "$OrigKey"
fi
使用以下命令调用它:
keyswap.sh '<full window class>' '<input key>' '<output key>'
例如:
keyswap.sh '"gnome-terminal", "Gnome-terminal"' 'F6' 'F7'