LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Hardware
User Name
Password
Linux - Hardware This forum is for Hardware issues.
Having trouble installing a piece of hardware? Want to know if that peripheral is compatible with Linux?

Notices


Reply
  Search this Thread
Old 10-24-2006, 10:57 AM   #1
arobinson74
Member
 
Registered: Nov 2002
Location: Lone Tree, CO
Distribution: Xubuntu Gutsy
Posts: 174

Rep: Reputation: 30
Running a custom script on USB keyboard hotplug event


I have a laptop for work that I use a USB keyboard with usually, but not always. When using the keyboard, I would like to have the NUM lock on, but when using the laptop keyboard, off. I was thinking it would be great to put a script in the hotplug configuration to automatically turn on/off the NUM lock when the keyboard is plugged in/removed.

Here is the script I would like to run (i.e. /etc/hotplug/usb/numlock):
Code:
#!/bin/sh

if [ "${ACTION}" = "add" ] ; then
  numlockx on
else
  numlockx off
fi
I have never setup a hotplug event such as this and was wondering for advice on how to detect if a USB keyboard has been added. Bascially, is there an easy way for a usermap to trap all keyboards. I could set it up for just my work keyboard, but that solution isn't as elegant as trapping all USB keyboards.

If someone could post such an example for a usermap file I would appreciate it.
 
Old 10-24-2006, 11:21 AM   #2
arobinson74
Member
 
Registered: Nov 2002
Location: Lone Tree, CO
Distribution: Xubuntu Gutsy
Posts: 174

Original Poster
Rep: Reputation: 30
Also, if there is a better way than to use the hotplug capabilities also let me know. I am not having much luck on learning how to register a hotplug script.
 
Old 10-24-2006, 02:42 PM   #3
arobinson74
Member
 
Registered: Nov 2002
Location: Lone Tree, CO
Distribution: Xubuntu Gutsy
Posts: 174

Original Poster
Rep: Reputation: 30
Lightbulb Solution

Okay, got a solution using udev (note that this applies to the debian configuration for file locations):

1) Create/edit a ~/.xsession file. Add the line (this will let the udev script running as root to modify the numlock in X):
Code:
xhost +local:
2) Create the udev rules script file (/etc/udev/usbkeyboard.rules):
Code:
SUBSYSTEM=="input", ENV{PHYSDEVDRIVER}=="usbhid", RUN+="/usr/local/bin/usbkeyboard-addremove"
3) Link the rules script so that it executes:
Code:
cd /etc/udev/rules.d; ln -snf ../usbkeyboard.rules 025_usbkeyboard.rules
4) Create the script that it calls, that does the work (/usr/local/bin/usbkeyboard-addremove):
Code:
#!/bin/sh

NL=/usr/bin/numlockx

if [ "${ID_CLASS}" == "kbd" ] ; then
  if [ "${DISPLAY}" == "" ] ; then
    export DISPLAY=":0.0"
  fi

  if [ "${ACTION}" = "add" ] ; then
    INITTY=/dev/tty[1-8]
    for tty in $INITTY; do
      setleds -D +num < $tty
    done

    ${NL} on
  else
    INITTY=/dev/tty[1-8]
    for tty in $INITTY; do
      setleds -D -num < $tty
    done

    ${NL} off
  fi
fi
Summary:

I had an issue for some reason to get the "add" event to match ENV{ID_CLASS}=="kbd", so that is why it is in the script and not in the rule. This script will not only change the numlock in X but also set the tty's numlock status.

Requirements:
Debian packages: kbd numlockx udev
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
cron is not workin well running a custom script on Slackware 10.1... sugar2 Linux - Software 3 08-24-2006 07:41 PM
run script at hotplug event AAnarchYY Linux - Hardware 1 07-21-2006 09:19 AM
hotplug script for usb-pen aspartat Linux - Software 2 02-20-2005 07:21 AM
Hotplug script for Usb drive Forced2Morph Linux - Hardware 0 10-15-2004 08:42 PM
RH9 usb mouse/keyboard hotplug men$an Linux - Distributions 2 07-16-2003 12:09 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Hardware

All times are GMT -5. The time now is 01:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration