
ノートパソコンを持っていますが、外付けの USB キーボードも接続されています。ノートパソコンの内蔵キーボードをロックして (つまり、キーを押しても何も反応しないようにして)、外付けキーボードは反応するようにすることは可能でしょうか?
私はUbuntu Gnome 16.04を実行しています。私のラップトップはLenovo ThinkPad T420です。
答え1
はい、 を使用すれば可能ですxinput
。
まず、xinput list
ターミナルで実行します。次のような画面が表示されます。
zachary@MCServer:~$ xinput list
⎡ Virtual core pointer id=2 [master pointer (3)]
⎜ ↳ Virtual core XTEST pointer id=4 [slave pointer (2)]
⎣ Virtual core keyboard id=3 [master keyboard (2)]
↳ Virtual core XTEST keyboard id=5 [slave keyboard (3)]
↳ Power Button id=6 [slave keyboard (3)]
↳ Power Button id=7 [slave keyboard (3)]
キーボードが 2 つ接続されているため、おそらく 1 つではなく 2 つのキーボードが表示されるはずです。USB キーボードを取り外してコマンドを実行することをお勧めします。
のIDをメモしてくださいVirtual core XTEST keyboard
。私の場合は です5
。
内部キーボードを無効にするので、USB キーボードを接続します。
次のコマンドを実行します:
xinput set-prop 5 "Device Enabled" 0
5 をデバイス ID に置き換えます。
キーボードを再度有効にするには:
xinput set-prop 5 "Device Enabled" 1`
5 をデバイス ID に置き換えます。
必要に応じて、これらを別のスクリプトに入れて、ターミナルから実行することもできます (または、.desktop
作成したスクリプトにファイルを作成することもできます)。
編集:
必要に応じて、指定されたデバイスの状態をチェックしxinput
て切り替える (オフの場合はオンに、オンの場合はオフに) スクリプトを作成しました。変数を対応する ID に変更する必要がありますdevice
。
#!/bin/bash
device=5 #Change this to reflect your device's ID
output=$(xinput list-props $device | awk '/Device Enabled/{print $4}')
if [ $output == 1 ]
then
xinput set-prop $device "Device Enabled" 0
elif [ $output == 0 ]
then
xinput set-prop $device "Device Enabled" 1
else
echo "Something's up."
fi
編集2:
スクリプトの改善 - デバイス ID の自動検出 (名前が固定されている場合) とデスクトップ通知。
#!/usr/bin/env bash
# name of the device - we hope this will not change
DEVNAME="AT Translated Set 2 keyboard"
# here we find the device ID corresponding to the name
device=$(xinput list | grep "${DEVNAME}" | sed "s/.*${DEVNAME}.*id=\([0-9]*\).*/\1/g")
# here we get the enabled state
state=$(xinput list-props $device | awk '/Device Enabled/{print $4}')
if [ ${state} == 1 ]; then
# if it is enabled, disable it and show a notification
xinput set-prop ${device} 'Device Enabled' 0
notify-send -u normal "Keyboard lock" "Keyboard \"${DEVNAME}\" (id=${device}) was disabled."
elif [ ${state} == 0 ]; then
# if it is disabled, enable it and show a notification
xinput set-prop ${device} 'Device Enabled' 1
notify-send -u normal "Keyboard lock" "Keyboard \"${DEVNAME}\" (id=${device}) was enabled."
else
# some weird state - do nothing and show critical notification
notify-send -u critical "Keyboard lock" "Keyboard \"${DEVNAME}\" (id=${device}) is neither enabled nor disabled. State is \"${state}\""
fi