Last active
October 18, 2018 13:33
-
-
Save sambrightman/90b4341c82c39a1a4bf3c1248f42f9c8 to your computer and use it in GitHub Desktop.
Keyboard toggle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Desktop Entry] | |
Version=1.0 | |
Type=Application | |
Name=Toggle Keyboard | |
Icon=gnome-dev-keyboard | |
Exec=/home/sam/bin/toggle_keyboard.sh | |
NoDisplay=false | |
Categories=Utility; | |
StartupNotify=false | |
Terminal=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# https://askubuntu.com/questions/160945/is-there-a-way-to-disable-a-laptops-internal-keyboard | |
# TODO: error check keyboard name/id | |
function mouse_id() { | |
local name=$1 && shift | |
xinput list --id-only "pointer:${name}" | |
} | |
function keyboard_id() { | |
local name=$1 && shift | |
xinput list --id-only "keyboard:${name}" | |
} | |
function keyboard_enabled() { | |
local id=$1 && shift | |
xinput list-props "${id}" | awk -F: '/Device Enabled/ { exit $2 != 1 }' | |
} | |
function enable_keyboard() { | |
local id=$1 && shift | |
local icon_path=~/Documents/keyboard-enable.png | |
notify-send -i "${icon_path}" "Enabling keyboard..." "keyboard id ${id}" | |
echo "enabling keyboard" | |
xinput enable "${id}" | |
if keyboard_enabled "${id}"; then | |
notify-send --icon "${icon_path}" "Enabling keyboard..." "ON - keyboard enabled" | |
echo "enabled keyboard" | |
else | |
notify-send --icon "${icon_path}" "Enabling keyboard..." "FAILED - keyboard still DISABLED" | |
echo "failed to enable keyboard" | |
fi | |
} | |
function disable_keyboard() { | |
local id=$1 && shift | |
local icon_path=~/Documents/keyboard-disable.png | |
notify-send -i "${icon_path}" "Disabling keyboard..." "keyboard id ${id}" | |
echo "disabling keyboard" | |
xinput disable "${id}" | |
if keyboard_enabled "${id}"; then | |
notify-send -i "${icon_path}" "Disabling keyboard..." "FAILED - keyboard still enabled" | |
echo "failed to disable keyboard" | |
else | |
notify-send -i "${icon_path}" "Disabling keyboard..." "OFF - keyboard disabled" | |
echo "disabled keyboard" | |
fi | |
} | |
function toggle_keyboard() { | |
local id=$1 && shift | |
if keyboard_enabled "${id}"; then | |
disable_keyboard "${id}" | |
else | |
enable_keyboard "${id}" | |
fi | |
} | |
# for some reason must do both | |
toggle_keyboard "$(keyboard_id "Corsair Corsair K70 RGB Gaming Keyboard")" | |
toggle_keyboard "$(mouse_id "Corsair Corsair K70 RGB Gaming Keyboard")" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment