NSLU2 Tools: keyevent

Purpose

'keyevent' is a little tool derived from evtest.c to report keys being pressed or released on a selectable hardware device. It uses the Linux kernel's event interface, so be sure to load the evdev kernel module (modprobe evdev) before using keyevent.

An example for the tool's output:

Double-clicking with the right button of a mouse:

> src/keyevent 09da 0006
Looking for device PID=09da VID=0006
Found event device #2
RightBtn pressed
RightBtn released
RightBtn pressed repeat
RightBtn released

For my Trust USB Sound Card pressing the MUTE button looks like:

> src/keyevent 145f 0090
Looking for device PID=145f VID=0090
Found event device #2
Mute pressed
Mute released

Ok, that's nice but not so useful without any application interpreting the output. The example below implements a shell script to control the Music Player Daemon (MPD) using just the MUTE button on the USB sound card mentioned above.

#!/bin/sh

MPD_HOST=localhost
MPD_PORT=6600
VID=145f
PID=0090

cmd_repeat="echo -e 'repeat 1\nclose' | nc $MPD_HOST $MPD_PORT > /dev/null"
cmd_playing="echo -e 'status\nclose' | nc $MPD_HOST $MPD_PORT | grep state: | grep -q play"
cmd_next="echo -e 'next\nclose' | nc $MPD_HOST $MPD_PORT > /dev/null"
cmd_stop="echo -e 'stop\nprevious\nclose' | nc $MPD_HOST $MPD_PORT > /dev/null"
cmd_play="echo -e 'play\nclose' | nc $MPD_HOST $MPD_PORT > /dev/null"

eval $cmd_repeat

keyevent $VID $PID 5000 1000 1000 10000 | \
( while :
  do
    read key state rep
    echo $key $state $rep
    if [ "$state" = "pressed" ]
    then
      if [ "$rep" = "double" ]
      then
        if eval $cmd_playing
        then
          echo "stopping"
          eval $cmd_stop
        fi
      else
        # single key press
        if eval $cmd_playing
        then
          echo "next song"
          eval $cmd_next
        else
          echo "starting"
          eval $cmd_play
        fi
      fi
    fi
  done )

Pressing the button once turns the player on ("PLAY") or, if it is already playing, skips to the next song in the playlist ("NEXT"). Pressing the button twice within 1 second stops the player ("STOP").

Download
keyevent 0.3 Source Code
keyevent 0.1 Source Code (outdated)
keyevent 0.1 BitBake Package

The packages are also available on my SlugOS/BE 3.10b feed. Follow these instructions (as root):

  1. Log into your NSLU2 and add the DevBase feed:
    # echo "src/gz devbase http://feed.devbase.at/slugos-3.10" > /etc/ipkg/01-devbase-feed.conf
  2. Update the package database:
    # ipkg update
  3. Install some package:
    # ipkg install some-package
3 comments so far (post your own)
[1] maybe you could help me with madplay+musicbrowser?
Posted by João on Saturday, 08.23.2008 @ 05:24AM
I have very basic knowledge of linux environment;still, I maanaged to install on my slug (unslung v. 5.5 beta) lighttpd and php-fcgi and then => mpd+madplay and the web front-end musicbrowser (php). It works fine with my usb philips speakers (I bought a sound usb-dongle for use with the slug). I just lack basic control from my laptop, e.g. to adjust the sound volume of the player from the web interface (from musicbrowser)...Is it possible to adapt a script so it could interact with the keyboard of a machine connected to the lan so it would be possible what I want?

If you have a little moment, I will appreciate any hint or sugestion from you
Regards
João
[2] 5.1 working on Trust USB device?
Posted by alan on Tuesday, 01.13.2009 @ 05:50AM
Hi there,

Great setup - I have a very similar home setup with my NSLU2, but haven't yet got the 5.1 sound working on my Trust USB sound card (same as yours). Have you got it working?

Cheers,
Alan
Reply by Thomas: Hi Alan,
a few months ago I moved my sound card from the NSLU2 to the QNAP TS-109 II. However I can confirm the following:
  • On the slug 2.0 was working fine.
  • Also on the slug I used dmix to create 2 independent virtual 2.0 sound cards where one was connected to the stereo and the other one to the mini-speaker (shown in my pictures). That also worked fine but create ~15% additional CPU usage by dmix. So after a year I bought a small dedicated CMedia sound card for the speaker.
  • I just tried "speaker-test" on the TS109 and it works fine in a 5.1 setup. I did not check whether there was an actual signal on the respective outputs though. This is without any /etc/asound.conf or ~/.asoundrc file, the ALSA lib and utils are version 1.0.17, kernel is 2.6.27.
So I would guess 5.1 sound should also work on the NSLU2. Do you use some special ALSA configuration? Is there a meaningful error message when you try something like "speaker-test -D plughw:0 -c 6 -f 48000"?

Best regards,
-Thomas
[3] thanks
Posted by Coleman on Friday, 02.13.2009 @ 07:06AM
hi i just wanted to leave you a message saying how much i really appreciate this software. im currently using this on my headless box and im using it with a psone controller to run/kill tasks when i want them to run.

thanks again for your hard work, good effort :)
Leave your comments
Name:
Email:
URL:
Subject:
Comments:
 

Note: The email address will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text. Examples:
[b]bold[/b] ... bold
[code]Source Code[/code] ... Source Code

Disclaimer:
The author does not take any responsibility for problems and damage arising by installing or using this software, use it at your own risk.