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,315
  • 12
  • 49
  • 65
  • Great! Seems it's working. One more question - can the device ID change between boots or is it guaranteed to be always the same for my particular configuration? – zegkljan Aug 31 '16 at 14:20
  • 1
    It should stay the same. It'll probably change if you reinstall, but just a reboot shouldn't affect it. Would you mind accepting this answer, since it worked? – TheWanderer Aug 31 '16 at 14:21
  • 1
    Oh, it seems like they might reset on reboot. This is beyond my knowledge for a script to adapt, but I may be able to get someone to help http://stackoverflow.com/questions/18755967/how-to-make-a-program-that-finds-ids-of-xinput-devices-and-sets-xinput-some-set/18756948#18756948 – TheWanderer Aug 31 '16 at 14:24
  • I'll accept it once I get my laptop back home and play with it a little bit more, don't worry ;-). Regarding the changing ID, I have a strong feeling that the name ("AT Translated Set 2 keyboard" in my case) won't change so I could ``sed`` the id out of it. Ugly, but I'm not controlling a nuclear power plant so no big deal :). – zegkljan Aug 31 '16 at 14:31
  • Oh good. At least you know what you're doing with scripts. I had to learn to write conditionals and variables in bash for that script :p. – TheWanderer Aug 31 '16 at 17:04