Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
use `cec-client` to fire events when keys are pressed
#!/usr/bin/env bash
#
# react to cec keypresses in the jankiest way possible
#
# Author: Dave Eddy <dave@daveeddy.com>
# Date: 10/15/2013
# Licens: MIT
# Tested on: Raspberry pi with libcec compiled from soure
onright() {
echo 'right button pressed'
}
onleft() {
echo 'left button pressed'
}
ondown() {
echo 'down button pressed'
}
onup() {
echo 'up button pressed'
}
onselect() {
echo 'select button pressed'
}
onplay() {
echo 'play button pressed'
}
onpause() {
echo 'pause button pressed'
}
onforward() {
echo 'forward button pressed'
}
onbackward() {
echo 'back button pressed'
}
filter() {
perl -nle 'BEGIN{$|=1} /key pressed: (.*) \(.*\)/ && print $1'
}
echo as | cec-client | filter | \
while read cmd; do
case "$cmd" in
right) onright;;
left) onleft;;
down) ondown;;
up) onup;;
select) onselect;;
play) onplay;;
pause) onpause;;
forward) onforward;;
backward) onbackward;;
*) echo "unrecognized button ($cmd)";;
esac
done

install libcec on raspbian

sudo apt-get update
sudo apt-get install build-essential autoconf liblockdev1-dev libudev-dev git libtool pkg-config
git clone https://github.com/Pulse-Eight/libcec
cd libcec
./bootstrap
./configure --with-rpi-include-path=/opt/vc/include --with-rpi-lib-path=/opt/vc/lib --enable-rpi
make
sudo make install
sudo ln -s /usr/local/lib/libcec.so /usr/lib/
sudo ln -s /usr/local/lib/libcec.so.2 /usr/lib/

run cec-keypresses.sh

~$ ./cec-keypresses.sh 
right button pressed
right button pressed
left button pressed
up button pressed
down button pressed
select button pressed
select button pressed
@bahamas10

This comment has been minimized.

Copy link
Owner Author

bahamas10 commented Oct 15, 2013

long term goal: make a c++ program that includes libcec and properly fires off external scripts when these events are caught.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.