[How-To] capture keyboard/mouse/buttons/IR events and execute actions

How to capture those events in a shell script?


Run a shell script on input device event

opkg update
opkg install evtest

example:
capture events from USB keyboard

evtest /dev/input/event0

Press the keys you want to capture

example results:

Testing ... (interrupt to exit)

key_play pressed:
Event: time 1579623858.490900, type 4 (EV_MSC), code 4 (MSC_SCAN), value 4b0
Event: time 1579623858.490900, type 1 (EV_KEY), code 28 (KEY_ENTER), value 0
Event: time 1579623858.490900, -------------- SYN_REPORT ------------

key_pause pressed:
Event: time 1579623859.906866, type 4 (EV_MSC), code 4 (MSC_SCAN), value 4ba
Event: time 1579623859.906866, type 1 (EV_KEY), code 32 (KEY_D), value 1
Event: time 1579623859.906866, -------------- SYN_REPORT ------------

key_stop pressed:
Event: time 1579623860.018862, type 4 (EV_MSC), code 4 (MSC_SCAN), value 4b1
Event: time 1579623860.018862, type 1 (EV_KEY), code 32 (KEY_D), value 0
Event: time 1579623860.018862, -------------- SYN_REPORT ------------

key_next pressed:
Event: time 1579623860.586844, type 4 (EV_MSC), code 4 (MSC_SCAN), value 48e
Event: time 1579623860.586844, type 1 (EV_KEY), code 29 (KEY_LEFTCTRL), value 1
Event: time 1579623860.586844, -------------- SYN_REPORT ------------

key_previous pressed:
Event: time 1579623860.834841, type 4 (EV_MSC), code 4 (MSC_SCAN), value 48f
Event: time 1579623860.834841, type 1 (EV_KEY), code 46 (KEY_C), value 1
Event: time 1579623860.834841, -------------- SYN_REPORT ------------

Bash script:

## to check events "type,code,value": 
#evtest /dev/input/event0-1-2-3-4, etc
#
device='/dev/input/event0'
key_play='*type 4 (EV_MSC), code 4 (MSC_SCAN), value 4b0*'
key_pause='*type 4 (EV_MSC), code 4 (MSC_SCAN), value 4ba*'
key_stop='*type 4 (EV_MSC), code 4 (MSC_SCAN), value 4b1*'
key_next='*type 4 (EV_MSC), code 4 (MSC_SCAN), value 48e*'
key_previous='*type 4 (EV_MSC), code 4 (MSC_SCAN), value 48f*'
evtest "$device" | while read line; do
    case $line in
        ($key_play)      echo "key Play";;
        ($key_pause)     echo "key Pause";;
        ($key_stop)      echo "key STOP";;
        ($key_next)      echo "key Next";;
        ($key_previous)  echo "Key Previous";;
    esac
done

Alternatives:

Triggerhappy

Hi pilovis.
I'm trying to install triggerhapy on Openwrt 18.06.1, and are receiving the message "Unknown package 'triggerhapy'."
Is this package obsolete?

Best,
carliedu

Please post the output of the following commands:

opkg update
opkg install triggerhappy
1 Like

Hi Thomas,
today the install has worked, only an error at the end on configutation step. I'll look for it.

D860wrt-09:24-~-> opkg update
Downloading http://downloads.openwrt.org/releases/18.06.1/targets/ramips/mt7621/packages/Packages.gz
Updated list of available packages in /var/opkg-lists/openwrt_core
Downloading http://downloads.openwrt.org/releases/18.06.1/targets/ramips/mt7621/packages/Packages.sig
Signature check passed.
Downloading http://downloads.openwrt.org/releases/18.06.1/packages/mipsel_24kc/base/Packages.gz
Updated list of available packages in /var/opkg-lists/openwrt_base
Downloading http://downloads.openwrt.org/releases/18.06.1/packages/mipsel_24kc/base/Packages.sig
Signature check passed.
Downloading http://downloads.openwrt.org/releases/18.06.1/packages/mipsel_24kc/luci/Packages.gz
Updated list of available packages in /var/opkg-lists/openwrt_luci
Downloading http://downloads.openwrt.org/releases/18.06.1/packages/mipsel_24kc/luci/Packages.sig
Signature check passed.
Downloading http://downloads.openwrt.org/releases/18.06.1/packages/mipsel_24kc/packages/Packages.gz
Updated list of available packages in /var/opkg-lists/openwrt_packages
Downloading http://downloads.openwrt.org/releases/18.06.1/packages/mipsel_24kc/packages/Packages.sig
Signature check passed.
Downloading http://downloads.openwrt.org/releases/18.06.1/packages/mipsel_24kc/routing/Packages.gz
Updated list of available packages in /var/opkg-lists/openwrt_routing
Downloading http://downloads.openwrt.org/releases/18.06.1/packages/mipsel_24kc/routing/Packages.sig
Signature check passed.
Downloading http://downloads.openwrt.org/releases/18.06.1/packages/mipsel_24kc/telephony/Packages.gz
Updated list of available packages in /var/opkg-lists/openwrt_telephony
Downloading http://downloads.openwrt.org/releases/18.06.1/packages/mipsel_24kc/telephony/Packages.sig
Signature check passed.
D860wrt-09:24-~-> opkg install triggerhappy
Installing triggerhappy (0.5.0-1) to root...
Downloading http://downloads.openwrt.org/releases/18.06.1/packages/mipsel_24kc/packages/triggerhappy_0.5.0-1_mipsel_24kc.ipk
Configuring triggerhappy.
Error opening '/dev/input/event*': No such file or directory
D860wrt-09:25-~->

Best,
carliedu

[Solved]
Hi Pilovis,

the error at the end

Error opening '/dev/input/event*': No such file or directory

was because there was no Keyboard attached on the router.

After attaching the USB keyboard, triggerhappy works fine.

Thanks for your effort.

Best,
carliedu

You can use inotifywait to determine if your /dev/input/* keyboard is connected. Here is what I do to determine when my Bluetooth headphones or speaker are connected and their buttons are available to control music playback.