[Android Software/Hacking General [Developers Only]] alecxs:
we can now use this GetKeyEvent() function for other architectures. i have wrapped arround example shell script for testing purposes. it runs only when called from /system/bin/sh, but you can just copy the function and fix for your needs. be aware to set path to /system/bin first, otherwise it may use /sbin/toolbox from TWRP which may not have getevent

edit: have re-written that function because some devices don't have proper input device for listening. on my device all three buttons exist on one single device (and only three) so the function is listening on this single event:

C:\Android>adb shell
CUBOT_KING_KONG:/ $ su
CUBOT_KING_KONG:/ # getevent -lc4 /dev/input/event2
EV_KEY KEY_VOLUMEDOWN DOWN
EV_SYN SYN_REPORT 00000000
EV_KEY KEY_VOLUMEDOWN UP
EV_SYN SYN_REPORT 00000000
CUBOT_KING_KONG:/ # getevent -li /dev/input/event2
add device 1: /dev/input/event2
bus: 0019
vendor 2454
product 6500
version 0010
name: "mtk-kpd"
location: ""
id: ""
version: 1.0.1
events:
KEY (0001): KEY_VOLUMEDOWN KEY_VOLUMEUP KEY_POWER

input props:
<none>
CUBOT_KING_KONG:/ #

unfortunately this is not the case for every phone model, so this function does not work

#!/system/bin/sh

# mksh for android: overwrite /sqlite_stmt_journals
export TMPDIR="/tmp"

# Shell Script Function: Recovery Flashable Zip: Read Phone Button [Vol+|Vol-|Pwr]
# aIecxs @ xda-developers
GetKeyEvent() {
local event vol_up vol_dn pwr

# get input device
[ -f "$TMPDIR/device.info" ] || toolbox getevent -il | busybox tac > "$TMPDIR/device.info"
[ -z "$event" ] && while read -r device_info
do
# find device
event=$(busybox grep -owE '(/dev/input/event[0-9]+)' <<< "$device_info")
if [ "$vol_dn" ] && [ "$vol_up" ] && [ "$pwr" ]
then
# all keys from same device -> exit
[ -z "$event" ] || break
elif [ "$event" ]
then
# not all keys present -> find next device
unset event
unset vol_dn
unset vol_up
unset pwr
fi
# key is present
busybox grep -q "KEY_VOLUMEDOWN" <<< "$device_info" && vol_dn=1
busybox grep -q "KEY_VOLUMEUP" <<< "$device_info" && vol_up=1
busybox grep -q "KEY_POWER" <<< "$device_info" && pwr=1
done < "$TMPDIR/device.info"
[ -z "$event" ] && ui_print "/dev/input/event not found - abort" && return 1

# now we have /dev/input/event we can listening for
# wait until key is released (read 4 lines)
[ "$event" ] && case "$(toolbox getevent -lc4 $event | busybox head -n1)" in
*KEY_VOLUMEDOWN*) busybox printf "KEY_VOLUMEDOWN";;
*KEY_VOLUMEUP*) busybox printf "KEY_VOLUMEUP";;
*KEY_POWER*) busybox printf "KEY_POWER";;
esac
return 0
}

new: universal function

#!/system/bin/sh

# mksh for android: overwrite /sqlite_stmt_journals
export TMPDIR="/tmp"

# Shell Script Function: Recovery Flashable Zip: Read Phone Button [Vol+|Vol-|Pwr]
# aIecxs @ xda-developers
GetKeyEvent() {
local input touchscreen found

# get input device for touch screen (we want to exclude)
[ -f "$TMPDIR/device.info" ] || toolbox getevent -il | busybox tac > "$TMPDIR/device.info"
[ -z "$touchscreen" ] && while read -r device_info
do
touchscreen=$(busybox grep -owE '(/dev/input/event[0-9]+)' <<< "$device_info")
if [ "$found" ]
then
[ -z "$touchscreen" ] || break
elif [ "$touchscreen" ]
then
unset touchscreen
unset found
fi
busybox grep -qi "TOUCH" <<< "$device_info" && found=1
done < "$TMPDIR/device.info"

# listen on all devices (exclude touch screen)
[ "$touchscreen" ] && unset input && until [ "$input" ]
do
input="$(toolbox getevent -lc2 | busybox grep ^/dev/input/event | busybox head -n1)"
case "$input" in
${touchscreen}*) unset input;;
*EV_KEY*KEY_VOLUMEDOWN*DOWN*) busybox printf "%s" "KEY_VOLUMEDOWN";;
*EV_KEY*KEY_VOLUMEUP*DOWN*) busybox printf "%s" "KEY_VOLUMEUP";;
*EV_KEY*KEY_POWER*DOWN*) busybox printf "%s" "KEY_POWER";;
*) unset input;;
esac
done

return 0
}

# Begin of shell script

# toolbox is required for getevent
busybox which toolbox &> /dev/null
if [ $? != 0 ]
then
ui_print "toolbox and/or busybox not found - abort"
exit 1
elif [ "$(toolbox getevent -h)" = "getevent: no such tool" ]
then
ui_print "$(busybox which toolbox) $(toolbox getevent)"
ui_print "toolbox not useable - abort"
exit 1
fi

# mksh is required for here strings (COMMAND <<< $WORD)
busybox ps | busybox grep /system/bin/sh | busybox grep -q $$
if [ $? != 0 ]
then
ui_print "usage: /system/bin/sh $0"
ui_print "run this script from mksh only - abort"
exit 1
fi

# example usage
ui_print "press any key"
key=$(GetKeyEvent) || exit 1
ui_print "$key"

ui_print "press another key"
key=$(GetKeyEvent) || exit 1
ui_print "$key"

ui_print "press a key again"
key=$(GetKeyEvent) || exit 1
ui_print "$key"

exit 0