Skip to content

Instantly share code, notes, and snippets.

@bahamas10
Last active January 16, 2022 13:02
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save bahamas10/6996290 to your computer and use it in GitHub Desktop.
Save bahamas10/6996290 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 <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
Copy link
Author

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

@tillbaks
Copy link

tillbaks commented Apr 1, 2020

Had to change to "key released" since my tv seems to send two key press events.

DEBUG:   [           82270]	SetCurrentButton F1 (blue) (71) D:0ms cur:71
DEBUG:   [           82270]	key pressed: F1 (blue) (71) current(ff) duration(0)
DEBUG:   [           82270]	Changed key F1 (blue) (71) D:0ms cur:ff
DEBUG:   [           82270]	key pressed: F1 (blue) (71, 0)
DEBUG:   [           82270]	CheckKeypressTimeout T:2141.038
DEBUG:   [           82270]	Key F1 (blue): idle (duration:0) (71) timeout:3225402ms (rel:500,rep:0,prs:500,rel:0)
DEBUG:   [           82270]	>> TV (0) -> Recorder 1 (1): user control pressed (44)
TRAFFIC: [           82359]	>> 01:8b:71
DEBUG:   [           82359]	key released: F1 (blue) (71) D:89ms
DEBUG:   [           82360]	>> TV (0) -> Recorder 1 (1): vendor remote button up (8B)```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment