4

I have a laptop but also an external USB keyboard connected to it. Is it somehow possible to lock the built-in laptop keyboard (i.e. keys pressed should have no effect) but keep the external one responsive?

I'm running Ubuntu Gnome 16.04. My laptop is Lenovo ThinkPad T420.

zegkljan
  • 428
  • 7
  • 19

1 Answers1

7

Yes, this should be possible, with the use of xinput.

To start, run xinput list in a terminal. You should see something similar to the following:

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)]

Now, you'll probably see two keyboards, instead of just one, since you have two keyboards plugged in. I recommend unplugging the USB keyboard and running the command.

Take note of the ID of Virtual core XTEST keyboard. In my case, it's 5.

Plug in the USB keyboard, since you're about to disable the internal one.

Run this command:

xinput set-prop 5 "Device Enabled" 0

and replace 5 with your device ID.

To re-enable the keyboard:

xinput set-prop 5 "Device Enabled" 1`

replace 5 with your device ID.

You can also put these in separate scripts if you want, and run those from the terminal (or create .desktop files to the scripts you make).

Edit:
If you want, I've made a script that will check the state of the specified device in xinput and toggle it (to on if off, to off if on). You will need to change the device variable to the corresponding 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

Edit 2:
Improved script - automatic device ID detection (provided its name is fixed) and desktop notifications.

#!/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
zegkljan
  • 428
  • 7
  • 19
TheWanderer
  • 19,525
  • 12
  • 52
  • 65