Skip to content

Instantly share code, notes, and snippets.

@afk-mario
Created January 16, 2019 06:44
Show Gist options
  • Save afk-mario/9019e91802771a9bb607cd584d83690d to your computer and use it in GitHub Desktop.
Save afk-mario/9019e91802771a9bb607cd584d83690d to your computer and use it in GitHub Desktop.
Disable laptop keyboard on external usb keyboard plugged on Linux
#!/bin/bash
# Make sure the file has execute permission $ chmod +x enable-keyboard.sh
# name of your laptop keyboard, you can get the name using $ xinput
kb_name='AT Translated Set 2 keyboard'
xinput disable "$kb_name"
/usr/bin/notify-send 'Laptop keyboard' 'disable 1' --icon=dialog-information --expire-time=4000
#!/bin/bash
# Make sure the file has execute permission $ chmod +x enable-keyboard.sh
# name of your laptop keyboard, you can get the name using $ xinput
kb_name='AT Translated Set 2 keyboard'
xinput enable "$kb_name"
/usr/bin/notify-send 'Laptop keyboard' 'enable' --icon=dialog-information --expire-time=4000
# place this file on /etc/udev/rules.d/local.rules
# using IMPORT{program} to import variables from xpub
# call a script placed at the the /home/USER/bin/
ACTION=="add", IMPORT{program}="/usr/bin/xpub", ENV{XAUTHORITY}="$env{XAUTHORITY}", ENV{DISPLAY}="$env{DISPLAY}", SUBSYSTEM=="usb", RUN+="/bin/su $env{XUSER} -c '/home/$env{XUSER}/bin/disable-keyboard.sh'"
ACTION=="remove", IMPORT{program}="/usr/bin/xpub", ENV{XAUTHORITY}="$env{XAUTHORITY}", ENV{DISPLAY}="$env{DISPLAY}", SUBSYSTEM=="usb", RUN+="/bin/su $env{XUSER} -c '/home/$env{XUSER}/bin/enable-keyboard.sh'"

Requirements

  • You need to have a working notify-send
  • You need to install xpub for getting current user session variables Github

Resources

Extra

  • You can use journalctl to check the udev logs and see if something is not working
  • Tried using a systemd service instead of RUN+= on udev rules but didn't managed to pass the variables of the current session to show notifications.
@bam80
Copy link

bam80 commented Jan 28, 2019

Thanks. Can this be helpful for Bluetooth keyboard?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment