Skip to content

Instantly share code, notes, and snippets.

@erkiesken
Created March 30, 2023 12:37
Show Gist options
  • Save erkiesken/2c543ee52263c61b7e9ca921ef08fd27 to your computer and use it in GitHub Desktop.
Save erkiesken/2c543ee52263c61b7e9ca921ef08fd27 to your computer and use it in GitHub Desktop.
Toggle built-in laptop keyboard on/off on Linux while using external keyboard on top of laptop
#!/usr/bin/env bash
set -euo pipefail
# Map hostname to its internal keyboard name.
# Find name with: xinput list
declare -A keyboards=( ["my-hostname"]="AT Translated Set 2 keyboard" )
id=0
host=$(hostname)
log() {
logger -s "${BASH_SOURCE}" "$@"
}
if [[ -v keyboards[$host] ]]; then
name="${keyboards[$host]}"
id=$(xinput list | grep "${name}" | cut -f2 | cut -f2 -d=)
log "Found keyboard '${name}' on host '${host}': id=${id}"
fi
if [ "${id}" = "0" ]; then
notify-send "No toggleable keyboard found"
exit
fi
enabled=$(xinput list-props "${id}" | grep "Device Enabled" | cut -f3)
if [ "${enabled}" = "0" ]; then
notify-send "Enabling keyboard..." \ "ON - keyboard connected";
log "Enabling keyboard '${name}'"
xinput enable "${id}"
elif [ "${enabled}" = "1" ]; then
notify-send "Disabling Keyboard" \ "OFF - keyboard disconnected";
log "Disabling keyboard '${name}'"
xinput disable "${id}"
else
log "Unknown keyboard state: ${enabled}"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment