Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'd like to run a program that records audio only when the Right Control key is held down. On keyboard release the result would write to disk as, e.g. yy-mm-dd hh:mm.ogg. How might I do that?

$ xmodmap -pke |grep -e Record
keycode 175 = XF86AudioRecord NoSymbol XF86AudioRecord

Right Control is captured as follows by xev for key down and key up events:

KeyPress event, serial 43, synthetic NO, window 0x1800001,
    root 0x7f, subw 0x0, time 27689430, (-145,169), root:(476,366),
    state 0x10, keycode 105 (keysym 0xffe4, Control_R), same_screen YES,
    XLookupString gives 0 bytes: 
    XmbLookupString gives 0 bytes: 
    XFilterEvent returns: False

KeyRelease event, serial 46, synthetic NO, window 0x1800001,
    root 0x7f, subw 0x0, time 27689540, (-145,169), root:(476,366),
    state 0x14, keycode 105 (keysym 0xffe4, Control_R), same_screen YES,
    XLookupString gives 0 bytes: 
    XFilterEvent returns: False

ClientMessage event, serial 46, synthetic YES, window 0x1800001,
    message_type 0x112 (WM_PROTOCOLS), format 32, message 0x110 (WM_DELETE_WINDOW)

FWIW, my current desktop is Xfce. One lead I'm following: http://www.thinkwiki.org/wiki/How_to_get_special_keys_to_work#bypassing_Xorg

share|improve this question
    
And what exactly doesn't work? If you manage to get halevt working, you should just need to start your recording on keypress and kill it on key release (not with SIGKILL, of course). –  peterph May 16 '13 at 15:42
    
Would running something as root (e.g. using sudo) be a problem? I don't know how to capture all events in an X environment, but I do know how to capture events from an /dev/input/event device (for which you'll need root privileges) –  brm Feb 13 at 21:40

2 Answers 2

For trapping the events, I reccommend xbindkeys, it works independently from your desktop environment. From there, you can trigger a shell script that does everything. The tricky bit is to do this press/release logic. One way is for xbindkeys to trigger an application that then by itself listens for release, but xorg events are unreliable and it may never exit (not to mention you need to code for xlib).

It's much better for the script to just start recording (with "safety" maximum length set) and write the procees id (PID) somewhere in the filesystem. The release event just reads this PID and uses kill to stop the recording. This is safe because arecord is designed to stop recording sensibly on kill signal.

A rough sketch:

  if [ ! -f "~/.recordpid" ]; then
    echo "already running" >&2
    exit 1
  fi
  file="$(date '+recording-%m-%d-%H:%M:%S.wav')"
  nohup arecord -f cd --duration=3600 "~/clips/$file" &> /dev/null &
  echo "$!" > "~/.recordpid"

You could run arecord output directly to lame or other encoding devices to avoid large files. Or convert after the release button event is sent. The release code could be

  if [ ! -f "~/.recordpid" ]; then
    pid="$(cat "~/.recordpid")"
    kill -l SIGTERM -- "$pid" &>/dev/null
    rm -f "~/.recordpid"
  fi

If you will encode on release, you need to get the filename somewhere, possibly from another temporary file.

share|improve this answer
    
7 lines of code, although I think It's worth learning it –  Xsi Mar 13 at 16:23

I don't know if Xfce supports triggering commands at the press and release of buttons, but I seem to recall that FVWM2 supports this. Using one script for starting the recording and another script for stopping it, this should be fully possible. arecord or parecord should be able to do the actual recording.

share|improve this answer

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.