Control mpg123 with Button Shim

Hi. I have been trying to make a Python script to control mpg123 with Button Shim. If you start mpg123 with the -C option you can use the “s” key for play/pause, “f” to skip to the next track, “d” to skip to the previous track and the +/- keys on the numeric pad to control volume levels. I would like to assign these to the five buttons on the Button Shim. Using the evdev library the corresponding keycodes for these keys are as follows. Their respectiv keycode numbers are in straight brackets.

KEY_S #play/pause [31]
KEY_F #next track [33]
KEY_D #prev track [32]
KEY_KPMINUS #volume down [74]
KEY_KPPLUS #volume up [78]

Could you point me in the direction of acomplishing this? Thank you in advance.

You can do this using /boot/config.txt. I have done this with the buttons of the pirate audio hat:

dtoverlay=gpio-key,gpio=5,keycode=103,label="UP"
dtoverlay=gpio-key,gpio=6,keycode=108,label="DOWN"
dtoverlay=gpio-key,gpio=16,keycode=106,label="RIGHT"
dtoverlay=gpio-key,gpio=24,keycode=105,label="LEFT"

Of course you have to adopt this to your needs.

Thank you for the tip, but there’s no documentation (I can find) as of what pin each button is connected to on the button shim.

I am sorry, this was an error on my side (I have two different button shims). This button-shim is connected to I2C, so they are not connected to GPIOs.

So you have two options: use the evdev library to synthesize key-events. Or use the option -R option of mpg123. This allows to control mpg123 via sysin (writing commands) instead of keys. Personally, I would try the first alternative first, it should be a bit simpler.