-
Notifications
You must be signed in to change notification settings - Fork 8
20170318_how to switch ctrl and caps lock on specific keyboard.html
CI edited this page Jun 5, 2018
·
1 revision
title: "如何只更換特定鍵盤的 Ctrl 和 Caps Lock 鍵位? on Ubuntu 16.04" date: 2017-03-18 type: blog author: 凍仁翔 link: http://note.drx.tw/2017/03/how-to-switch-ctrl-and-caps-lock-on-specific-keyboard.html layout: post comments: true
四年前,凍仁找到了對調 Ctrl 和 Caps Lock 的方法,並紀錄於「對調 Ctrl 和 Caps Lock 鍵位」一文,但使用 setxkbmap 指令操作時,會連外接不需更換鍵位的 HHKB Pro 2 也一併生效,害得凍仁每次都得重新插拔 USB 線來排除此問題;而現在我們只需透過 -device 參數即可對特定鍵盤進行操作。
1. 使用 xinput 列出所有鍵盤輸入裝置,此為 ThinkPad T410 外接 HHKB Pro 2 之結果。
▲ ThinkPad T410 內建鍵盤的裝置名稱為 AT Translated Set 2 keyboard,而外接的 HHKB Pro 2 則是 Topre Corporation HHKB Professional。[ jonny@xenial]
$ xinput -list | grep -i key [Enter]
⎣ Virtual core keyboard id=3 [master keyboard (2)]
↳ Virtual core XTEST keyboard id=5 [slave keyboard (3)]
↳ Power Button id=6 [slave keyboard (3)]
↳ Video Bus id=7 [slave keyboard (3)]
↳ Sleep Button id=8 [slave keyboard (3)]
↳ Integrated Camera id=12 [slave keyboard (3)]
↳ AT Translated Set 2 keyboard id=13 [slave keyboard (3)]
↳ ThinkPad Extra Buttons id=16 [slave keyboard (3)]
↳ ACPI Virtual Keyboard Device id=17 [slave keyboard (3)]
↳ Topre Corporation HHKB Professional id=9 [slave keyboard (3)]
2. 藉由 setxkbmap 指令搭配裝置代號 (id) 使用 即可對特定特定鍵盤進行操作。
[ jonny@xenial ]
$ /usr/bin/setxkbmap -option "ctrl:swapcaps" -device 13 [Enter]
# 此例中,裝置代號 13 指的是 ThinkPad T410 的內建鍵盤,請依自行需求進行更改。
3. 最後凍仁寫了個簡易的 Shell Script,並讓它在登入時自動執行。
[ jonny@xenial ~ ]▲ Reference: hacking-ubuntu.ansible / files / switch-capslock-and-ctrl.sh
$ vi switch-capslock-and-ctrl.sh [Enter]#!/bin/bash
# Find the build in thinkpad keyboard device id.
BUILDIN_KEYBOARD_ID=$(xinput list | grep 'AT Translated Set 2 keyboard' | awk '{ print $7 }' | sed 's/id=//')
HHKB_COUNT=lsusb </span><span class="Structure">|</span><span class="Special"> </span><span class="Statement">grep</span><span class="Special"> </span><span class="Structure">"</span><span class="String">Topre Corporation HHKB Professional</span><span class="Structure">"</span><span class="Special"> </span><span class="Structure">|</span><span class="Special"> wc </span><span class="Special">-l</span><span class="Special">
if [ $HHKB_COUNT -ne "1" ]; then
/usr/bin/setxkbmap -option "ctrl:swapcaps"
else
# Only switch the Caps Lock and Left Ctrl layout at ThinkPad T410.
/usr/bin/setxkbmap -option "ctrl:swapcaps" -device $BUILDIN_KEYBOARD_ID
fi
現在我們再也不用重新插拔 USB 線了,Happy hacking our keyboard !
站內連結:
★ 對調 Ctrl 和 Caps Lock 鍵位
資料來源:
★ linux - Two keyboards on one computer. When I write with A I want a US keyboard layout, when I use B I want Swedish. Possible? | Super User