Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Every time I plug in an external keyboard to my laptop the keyboard repeat and delay are set to the slow default. My current workaround is to open the keyboard settings dialog and slightly move the delay slider. This is annoying and happens to me every day so I'd like a better/faster solution.

This bug report from Karmic is exactly what's happening to me, but I'm running xfce4 on 12.10 and not gnome on Karmic.

Is there a script I could write to reload the xfce4 settings anytime a usb keyboard is plugged in? (udev rule?)

Any help in solving this problem is appreciated.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

I know this is a dated problem, but it STILL exists till today. See e.g.

https://bugs.launchpad.net/ubuntu/+source/xfce4-settings/+bug/1180120

Until the xfce guys can get this straight, here are some sample scripts that could be used to fix the problem. It applies to a single-xfce4-session kind of situations (which covers the majority of user case), i.e. no two or three simultaneous X sessions.

The first is file /etc/udev/rules.d/50-external-keyboard.rules, which invokes another script upon insertion of a particular USB keyboard (you'll need to adjust your idVendor and idProduct numbers):

SUBSYSTEM=="usb", ATTR{idVendor}=="045e", ATTR{idProduct}=="071d", RUN+="/etc/udev/_Actions/x-keyboard-rates-launcher.sh"

The launcher script is a proxy launcher which drops the privilege to the user level for security reasons. Save this as /etc/udev/_Actions/x-keyboard-rates-launcher.sh and adjust X_USER to your name:

#!/bin/sh
# Adapted from http://unix.stackexchange.com/questions/65891/how-to-execute-a-shellscript-when-i-plug-in-a-usb-device

# Set DEBUG to something non-null string if we want to debug the script.
DEBUG=
X_USER=wirawan
export DISPLAY=:0
export XAUTHORITY="/home/$X_USER/.Xauthority"

Log () {
  if [ -n "$DEBUG" ]; then
    echo "$*" >> /tmp/udev_test_action.log
  fi
}

Log "$ACTION : $(date)"

if [ "${ACTION}" = "add" ]
then
  if [ -n "$DEBUG" ]; then
    export
    su -c "/bin/sh /etc/udev/_Actions/x-keyboard-rates-user.sh $DISPLAY $X_USER >> /tmp/udev_test_action.$X_USER.log 2> /tmp/udev_test_action.$X_USER.err" $X_USER &
  else
    su -c "/bin/sh /etc/udev/_Actions/x-keyboard-rates-user.sh $DISPLAY $X_USER > /dev/null 2>&1" $X_USER &
  fi
fi

Then this is the real script that reapply your keyboard parameters based on your own xfce settings. Save this as /etc/udev/_Actions/x-keyboard-rates-user.sh:

#!/bin/sh
# 20150318
# This script is supposed to run on the user level, not as the root.

if [ -n "$DEBUG" ]; then
  set -x
fi

X_USER=${2:-wirawan}
export DISPLAY=${1:-:0}
export XAUTHORITY=${3:-/home/$X_USER/.Xauthority}

is_x_running () {
# Detects whether the X server is up and running on the
# given display
  xdpyinfo > /dev/null 2>&1
}

is_user_session_up () {
# Detects whether the X session of interest is up
  pgrep xfce4-session -u $X_USER  > /dev/null 2>&1
}

get_keyboard_settings () {
  /usr/bin/xfconf-query -c keyboards -p /Default/KeyRepeat/Delay  \
    && /usr/bin/xfconf-query -c keyboards -p /Default/KeyRepeat/Rate 
}

apply_keyboard_settings () {
  if [ $# -eq 2 ]; then
    /usr/bin/xset r rate $1 $2
  else
    return 2
  fi
}

Log () {
  if [ -n "$DEBUG" ]; then
    echo "$*"
  fi
}

Log "$ACTION :user: $(date)"

if [ "${ACTION}" = "add" ]
then
  sleep 1s
  if is_x_running; then
    if is_user_session_up; then
      KB_SETTINGS=$(get_keyboard_settings) || {
        Log "Error: cannot get keyboard settings"
        exit 1
      }
      apply_keyboard_settings $KB_SETTINGS || {
        Log "Error: cannot apply keyboard settings"
        exit 1
      }
    else
      Log "Warning: target user session is not up; quitting"
    fi
  else
    Log "Warning: X is not running; quitting"
  fi
fi

Good luck!

Wirawan

share|improve this answer
    
I think I just got it to work, here are some more instructions you might want to add for future users: X_USER=${2:-wirawan} should be changed to your user name in /etc/udev/_Actions/x-keyboard-rates-user.sh. Both of the scripts need to be set to executable: sudo chmod +x /etc/udev/_Actions/x-keyboard-rates-*. To refresh udev without restarting I ran: sudo udevadm control --reload-rules; sudo udevadm trigger. –  Tron 2 days ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.