Lubuntu: ディスプレイを切り替えるホットキー

Lubuntu: ディスプレイを切り替えるホットキー

私は Linux も bash スクリプトも比較的初心者です。

古い Asus EEE ネットブックで Fn-F5 キーを設定して、内部モニターと外部モニターを切り替えようとしています。

bash スクリプトの作成~/bin/toggle_displays.sh:

    #!/bin/bash

if (xrandr | grep "VGA1 disconnected"); then
# external monitor not connected: do nothing
    xrandr --output LVDS1 --auto --output VGA1 --off 
else
# external monitor connected: toggle monitors
    if (xrandr | grep -E "LVDS1 connected (primary )?\("); then
        xrandr --output LVDS1 --auto --output VGA1 --off
    else
        xrandr --output LVDS1 --off --output VGA1 --auto
    fi
fi

<keyboard>セクションにホットキーを追加します~/.config/openbox/lubuntu-rc.xml:

...
<keybind key="XF86Display">
  <action name="Execute">
    <command>~/bin/toggle_displays.sh</command>
  </action>
</keybind>
...

問題は非常に奇妙です。内部ディスプレイがアクティブな場合、外部ディスプレイへの切り替えは常に機能します。しかし、外部ディスプレイがアクティブな場合、切り替えると、内部ディスプレイにマウス カーソルがある黒い画面が表示されるだけです。さらに驚くべきことに、後者の切り替えは、ホットキーではなくターミナルからスクリプトを実行すると機能することがあります。

何が問題なのでしょうか? これをデバッグするにはどうすればよいでしょうか?

補足ですが、ターミナルからスクリプトを実行して外部ディスプレイから内部ディスプレイに切り替えると、ターミナルに出力されます。スクリプトでLVDS1 connected primary (normal left inverted right x axis y axis)実行していないのに、なぜこれが起こるのでしょうか?echo

編集: これが私のxrandr出力です (外部モニターを使用)。

Screen 0: minimum 8 x 8, current 1680 x 1050, maximum 32767 x 32767
LVDS1 connected primary (normal left inverted right x axis y axis)
   1024x600       60.0 +
   800x600        60.3     56.2  
   640x480        59.9  
VGA1 connected 1680x1050+0+0 (normal left inverted right x axis y axis) 473mm x 296mm
   1680x1050      60.0*+
   1600x1200      60.0  
   1280x1024      75.0     60.0  
   1440x900       75.0     59.9  
   1280x800       59.8  
   1152x864       75.0  
   1024x768       75.1     70.1     60.0  
   832x624        74.6  
   800x600        72.2     75.0     60.3     56.2  
   640x480        75.0     72.8     66.7     60.0  
   720x400        70.1  
VIRTUAL1 disconnected (normal left inverted right x axis y axis)

答え1

私は、ラップトップの画面と外部画面を切り替える bash スクリプトを自分で書きました。このスクリプトは、どちらの画面がオンになっているかをチェックし、その画面をオフにして、もう一方の画面をネイティブ解像度でオンにします。便利なのは、画面の名前は xrandr から収集されるため、それを知る必要がないことです。

#!/bin/bash
#Toggles between two screens. Assuming only one external screen is connected

monitors=`xrandr | grep -P ' connected' | grep -o '^[^ ]*'`
set -- "$monitors"
#monArr contains an array of the id's of the connected screens
declare -a monArr=($*)

#onMon holds the id of the *first* on screen found
onMon=`xrandr --listmonitors | head -2 | grep -oP "[a-zA-Z]*-[0-9]$"`

#offMon holds the id of the other monitor, which is not on
if [ "$onMon" = "${monArr[0]}" ]

then
    offMon=${monArr[1]}
else
    offMon=${monArr[0]}
fi

#Switches the on screen off and vice versa
xrandr --output $offMon --auto --output $onMon --off

関連情報