Skip to content

Instantly share code, notes, and snippets.

@itshaadi
Forked from bahamas10/cec-keypresses.sh
Last active March 9, 2018 18:35
Show Gist options
  • Save itshaadi/fe71b3d6d579a33d12fa5f6959b140f7 to your computer and use it in GitHub Desktop.
Save itshaadi/fe71b3d6d579a33d12fa5f6959b140f7 to your computer and use it in GitHub Desktop.
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 <[email protected]>
# 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 released: (.*) \(.*\)/ && 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment