I've been on a quest to "map" some bluetooth speaker buttons to corrospond to the letters "d" and "f" to a non-existent keyboard. That is, there is no physical keyboard connected to my raspberry-pi. I've been spinning my wheel, to say the least.
The bluetooth speaker I have, has three button on top of it. A button to play andpause music, and a button to go forward, and finally, a button to go to the previous track.
When using the evdev module, I can view these buttons when they are pressed:
from evdev import InputDevice, categorize, ecodes dev = InputDevice('/dev/input/event0') print(dev) for event in dev.read_loop(): if event.type == ecodes.EV_KEY: print(categorize(event))
key event at 1465055027.204988, 163 (KEY_NEXTSONG), down key event at 1465055027.206383, 163 (KEY_NEXTSONG), up key event at 1465055028.666699, 165 (KEY_PREVIOUSSONG), down key event at 1465055028.668083, 165 (KEY_PREVIOUSSONG), up
Minus the (KEY_PLAYCD) output. I left it out intentionally.
Below is some code I cobbled together, which I believe is close. I'm trying to "inject" or "write" the key "d", which is the "track forward" in "mpg123". When I run "mpg123" (from the command line) with the "-C" option, I can press the key "d" to move the track forward. Also, key "f" moves the track back. I would like to map these keys to the "KEY_NEXTSONG" and the "KEY_PREVIOUSSONG" (ecodes?). Is it possible? Here some code I'm using:
#!/usr/bin/python # from subprocess import Popen, PIPE from evdev import InputDevice, categorize, ecodes dev = InputDevice('/dev/input/event0') print(dev) # Start mpg123 p = Popen(['mpg123', '-C', '--list', '/home/pi/bluemusic/playlist.txt','random'] , stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True) def getKey(): for event in dev.read_loop(): if event.type == ecodes.EV_KEY: c = categorize(event) if c.keystate == c.key_down: yield c.keycode keygenerator = getKey() while True: c = next(keygenerator) if c == 'KEY_NEXTSONG': p.stdin.write(ecodes.KEY) if c == 'KEY_PREVIOUSSONG': p.stdin.write(ecodes.KEY if c == 'KEY_PLAYCD': print(c) #This doesn't output??? #p.stdin.write(' ') # sends a space to the running process
I know this code is miserably incorrect. When it comes to printing these events. That has been accomplished. I'm ready to start doing something useful with these buttons. Any help or suggestions would be appreciated.