0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

keydを設定したt2linuxにおける、macbookのTouchBarの表示切り替え

0
Last updated at Posted at 2026-03-08

Touch Barのカスタマイズ:制御スクリプトと設定ファイルの適用手順

T2 Mac上でLinuxを動かす際、デフォルトではTouch Barが固定表示になりがちです。これを「Fnキー(Caps Lock等)」で動的に切り替え可能にするための、実践的な設定手順をまとめます。


ステップ1:表示レイアウトの定義 (/etc/tiny-dfr/config.toml)

tiny-dfr は、Touch Barの表示内容を司るデーモンです。まずは、システム標準のテンプレートをユーザー設定ディレクトリにコピーして編集します。

1. 設定ディレクトリの作成とコピー

既製品(/usr/share)を、特注品置き場(/etc)へコピーします。

sudo mkdir -p /etc/tiny-dfr
sudo cp /usr/share/tiny-dfr/config.toml /etc/tiny-dfr/config.toml

2. 設定ファイルの編集
コピーしたファイルを sudo nano /etc/tiny-dfr/config.toml で開き、自分好みに書き換えます。

/etc/tiny-dfr/config.toml
# tiny-dfr config template. Do not edit this file directly, instead
# copy it to /etc/tiny-dfr/config.toml and edit that copy.
# The daemon will merge those two files, giving preference to the one in /etc

# F{number} keys are shown when Fn is not pressed by default.
# Set this to true if you want the media keys to be shown without Fn pressed
MediaLayerDefault = false

# Set this to false if you want to hide the button outline,
# leaving only the text/logo
#ShowButtonOutlines = true

# Set this to true to slowly shift the entire screen contents.
# In theory this helps with screen longevity, but macos does not bother doing it
# Disabling ShowButtonOutlines will make this effect less noticeable to the eye
#EnablePixelShift = false

# Set this to the fontconfig pattern to be used to pick a font for text labels
# Some examples are:
# "" - default regular sans-serif font
# ":bold" - default bold sans-serif font
# For full reference on accepted values see the fontconfig user guide,
# section "Font Names"
# https://www.freedesktop.org/software/fontconfig/fontconfig-user.html
#FontTemplate = ":bold"

# Set this to false if you want the brightness of the touchbar
# to be set to a static value instead of following the primary
# screen's brightness
#AdaptiveBrightness = true

# With adaptive brightness disabled this is used as the brightness
# in the active state
# With it enabled, this is the maximum point on the brightness curve
# Accepted values are 0-255
#ActiveBrightness = 128

# This key defines the contents of the primary layer
# (the one with F{number} keys)
# You can change the individual buttons, add, or remove them
# Any number of keys that is greater than 0 is allowed
# however rendering will start to break around 24 keys
# Buttons can be made larger by setting the optional Stretch field
# to a number greater than 1 (which means the button will take up
# that many button spaces).
PrimaryLayerKeys = [
    # Action defines the key code to send when the button is pressed
    # Text defines the button label
    # Icon specifies the icon to be used for the button.
    # Theme specifies the XDG icons theme.
    # Stretch specifies how many button spaces the button should take up
    # and defaults to 1
    # Icons can either be svgs or pngs, with svgs being preferred
    # For best results with pngs, they should be 48x48
    # Do not include the extension in the file name.
    # If a Theme is set, icons are looked up in XDG_DATA_DIRS.
    # Otherwise, they are first looked up in /etc/tiny-dfr, and then in /usr/share/tiny-dfr.
    # Time can be either 24hr, or 12hr. Locale is optional and will default to POSIX.
    # Only one of Text, Icon or Time is allowed,
    # if both are present, the behavior is undefined.
    # For the list of supported key codes see
    # https://docs.rs/input-linux/latest/input_linux/enum.Key.html
    # Note that the escape key is not specified here, as it is added
    # automatically on Macs without a physical one
    { Text = "F1",  Action = "F1"  },
    { Text = "F2",  Action = "F2"  },
    { Text = "F3",  Action = "F3"  },
    { Text = "F4",  Action = "F4"  },
    { Text = "F5",  Action = "F5"  },
    { Text = "F6",  Action = "F6"  },
    { Text = "F7",  Action = "F7"  },
    { Text = "F8",  Action = "F8"  },
    { Text = "F9",  Action = "F9"  },
    { Text = "F10", Action = "F10" },
    { Text = "F11", Action = "F11" },
    { Text = "F12", Action = "F12" }

    # Example with Stretch:
    # # because most buttons have stretch 2, they behave as if they all had 1:
    # { Text = "F1",  Action = "F1", Stretch = 2  },
    # { Text = "F2",  Action = "F2", Stretch = 2  },
    # # these two buttons are half the size of the other buttons:
    # { Text = "F3",  Action = "F3", Stretch = 1  },
    # { Text = "F4",  Action = "F4", Stretch = 1  },
    # { Text = "F5",  Action = "F5", Stretch = 2  },
    # { Text = "F6",  Action = "F6", Stretch = 2  },
    # { Text = "F7",  Action = "F7", Stretch = 2  },
    # # these two buttons are one and a half the size of the other buttons:
    # { Text = "F8",  Action = "F8", Stretch = 3  },
    # { Text = "F9",  Action = "F9", Stretch = 3  },
    # { Text = "F10", Action = "F10", Stretch = 2 },
    # { Text = "F11", Action = "F11", Stretch = 2 },
    # { Text = "F12", Action = "F12", Stretch = 2 }

    # Example of Time:
    # { Time = "12hr",  Action = "Time"},
    # Example of Time with locale:
    # { Time = "12hr",  Locale = "en_IN", Action = "Time"},
    # Example of Time with stretch:
    # # the time key by default will be 3 times wider than the rest keys.
    # # So the stretch value assigned will be 3 times the regular keys.
    # { Time = "12hr",  Action = "Time", Stretch = 2},
    # # Here, time key will be 3x2=6 times the normal keys.
]

# This key defines the contents of the media key layer
MediaLayerKeys = [
    { Icon = "brightness_low",  Action = "BrightnessDown" },
    { Icon = "brightness_high", Action = "BrightnessUp"   },
    { Icon = "mic_off",         Action = "MicMute"        },
    { Icon = "search",          Action = "Search"         },
    { Icon = "backlight_low",   Action = "IllumDown"      },
    { Icon = "backlight_high",  Action = "IllumUp"        },
    { Icon = "fast_rewind",     Action = "PreviousSong"   },
    { Icon = "play_pause",      Action = "PlayPause"      },
    { Icon = "fast_forward",    Action = "NextSong"       },
    { Icon = "volume_off",      Action = "Mute"           },
    { Icon = "volume_down",     Action = "VolumeDown"     },
    { Icon = "volume_up",       Action = "VolumeUp"       }

    # Example with XDG icons (requires `breeze-dark` theme installed):
    # { Icon = "brightness-low",       Theme = "breeze-dark", Action = "BrightnessDown" },
    # { Icon = "brightness-high",      Theme = "breeze-dark", Action = "BrightnessUp"   },
    # { Icon = "microphone",           Theme = "breeze-dark", Action = "MicMute"        },
    # { Icon = "search",               Theme = "breeze-dark", Action = "Search"         },
    # { Icon = "redshift-status-day",  Theme = "breeze-dark", Action = "IllumDown"      },
    # { Icon = "redshift-status-on",   Theme = "breeze-dark", Action = "IllumUp"        },
    # { Icon = "media-skip-backward",  Theme = "breeze-dark", Action = "PreviousSong"   },
    # { Icon = "media-playback-start", Theme = "breeze-dark", Action = "PlayPause"      },
    # { Icon = "media-skip-forward",   Theme = "breeze-dark", Action = "NextSong"       },
    # { Icon = "audio-volume-muted",   Theme = "breeze-dark", Action = "Mute"           },
    # { Icon = "audio-volume-low",     Theme = "breeze-dark", Action = "VolumeDown"     },
    # { Icon = "audio-volume-high",    Theme = "breeze-dark", Action = "VolumeUp"       }
]

3. 反映テスト
設定を読み込ませるため、デーモンを再起動します。

sudo systemctl restart tiny-dfr

ステップ2:切り替えスクリプトの作成 (/usr/local/bin/fn_toggle-touchbar)

次に、特定のキー入力をトリガーにして表示モードを切り替えるロジックを作成します。

1. スクリプトファイルの作成

.bash
sudo nano /usr/local/bin/fn_toggle-touchbar
./usr/local/bin/fn_toggle-touchbar
#!/bin/bash
CONFIG="/etc/tiny-dfr/config.toml"

# MediaLayerDefault の行を探して true か false かで分岐
if grep -q "MediaLayerDefault = true" "$CONFIG"; then
   sudo sed -i 's/MediaLayerDefault = true/MediaLayerDefault = false/' "$CONFIG"
   echo "F-Key Mode"
else
   sudo sed -i 's/MediaLayerDefault = false/MediaLayerDefault = true/' "$CONFIG"
   echo "Media Mode"
fi

# tiny-dfrを再起動して反映
#sudo systemctl restart tiny-dfr
# restartがいらないことがわかった(config.tomlを書き換えるだけで切り替わる)

2. 実行権限の付与
一度 chmod +x を行えば、以降中身を書き換えても権限は維持されます。

sudo chmod +x /usr/local/bin/fn_toggle-touchbar

ステップ3:キーイベントとの紐付け (keyd等の設定)

作成したスクリプトを実際のキー操作に割り当てます。

設定例 (/etc/keyd/default.conf 等):

.例1
[main]
capslock = timeout(capslock, 200, layer(fn))

[fn]
# 例:Fnキー(Caps Lock)と組み合わせてスクリプトを実行
f1 = command(/usr/local/bin/fn_toggle-touchbar)
.例2
[main]
capslock = command(/usr/local/bin/fn_toggle-touchbar)

まとめ:なぜこの手順なのか?

  • /usr/share/ からのコピー: ツールが期待する最新のデフォルト設定をベースにできるため、記述ミスを防げます。
  • /etc/ での編集: システムアップデート時に設定が上書きされるのを防ぎ、自分のカスタムを最優先させます。
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?