Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kaosf/ae4bc52dd78dbbf730f2f0eae3f76ffa to your computer and use it in GitHub Desktop.
Save kaosf/ae4bc52dd78dbbf730f2f0eae3f76ffa to your computer and use it in GitHub Desktop.
How to run "xmodmap" command when a Bluetooth keyboard is reconnected.
# Check your Bluetooth keyboard MAC Address by
sudo bluetoothctl devices
# Prepare .xmodmap file.
# It crashes "xmodmap $HOME/.xmodmap" command when there is no Caps_Lock keysym.
# Prepare a script file of "reconnect.sh", and change the MAC address placeholder and some commands in it.
# Run this command.
sudo btmon | bash reconnect.sh
# Press Ctrl+C to stop it.
# Change <YOUR_MAC_ADDRESS> and add or remove the commands in the deepest "if" block as you like.
while read line; do
if echo $line | grep -q 'Connect Complete'; then
read line
if echo $line | grep -q 'Status: Success'; then
read line
read line
if echo $line | grep -q '<YOUR_MAC_ADDRESS>'; then
while :; do
sleep 1
if xmodmap $HOME/.xmodmap; then
xset r rate 170 130 # Remove this command if you don't need it.
# And add any other commands as you like.
break
fi
done
fi
fi
fi
done
remove Lock = Caps_Lock
keysym Caps_Lock = Control_L
add Control = Control_L
"btmon" command continues to output Bluetooth log messages.
When a keyboard is connected, there are some log messages like following:
...
> HCI Event: Connect Complete (0x03) plen 11 #96 [hci0] 20.027558
Status: Success (0x00)
Handle: 512
Address: 01:23:45:AB:CD:EF (DARFON LIGHTING CORP)
Link type: ACL (0x01)
Encryption: Disabled (0x00)
...
But, it seems that the keyboard is not detected immediately.
I guess "xmodmap .xmodmap" command fails because Caps_Lock key doesn't exist yet.
So I make this script to wait a Caps_Lock key resurrection by "sleep 1" loop.
If the log message style is different on your environment, and so the script doesn't work well,
please fix "reconnect.sh" for your environment. I believe you can do it!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment