鎖定內建筆記型電腦鍵盤,但保持外部 USB 鍵盤正常運作

鎖定內建筆記型電腦鍵盤,但保持外部 USB 鍵盤正常運作

我有一台筆記型電腦,但也連接了一個外部 USB 鍵盤。是否可以以某種方式鎖定內建筆記型電腦鍵盤(即按下的按鍵應該不起作用)但保持外部鍵盤響應?

我正在運行 Ubuntu Gnome 16.04。我的筆記型電腦是聯想 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)]

現在,您可能會看到兩個鍵盤,而不是只有一個,因為您插入了兩個鍵盤。

記下 的 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並切換它(如果關閉則打開,如果打開則關閉)。您需要將device變數變更為相應的 ID。

#!/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

相關內容