Want extra keys? Use your earbuds
Published onHave you ever wanted to set random actions to your keyboard keys but realized that you use them all for other purposes, like writing? Well, with this little trick, you can use your earbuds (in an unconventional way) to run custom actions on your computer.
Earbuds
I have a pair of Sony earbuds that have touch controls on both sides. On my phone, I can use them to control the media playback, and some other actions that I completely forgot about. However, those actions are not customizable, at least not on my computer. So by default, we have 3 gestures: single tap, double tap, and triple tap. These gestures are assigned to the media playback controls as play/pause, next, and previous respectively. But that’s not what we want. You’re with me, right? Let’s get started.
Media Session API
I tried to listen for Bluetooth events on my computer but it was way too complicated. Then I remembered about the Media Session API, which is a web API to control the media playback on your computer. You can set some metadata (which shows up on your control center) and even listeners for various events like play, pause, next, and previous. You see where I’m going with this. We’ll just put together a simple web page with a dummy audio element and listen for the events and send them over to our Python script to run functions as background tasks.
I hope you’re not bored with Quart yet because that’s what you’re getting from me again.
The Prototype
There is a hidden audio element below the button, which gets played when you click the button. By clicking on the gestures, you can select one of the custom actions to assign it to that gesture.
And when you toggle the switch, the page starts listening for the events without messing up with other media playing on your computer. Now’s the time to ask, what can you do with this? Well, I asked the same question to mysql (little typo there, but I’m keeping it because it’s funny) and came up with these terrible ideas:
- Take a screenshot with a single tap
- Use the next and previous gestures to switch between workspaces
- Open up spotify with a triple tap
- Open up a terminal with a single tap (this one sounds cool actually)
- Perform keyboard combinations with xdotool
Some of these are already implemented in the code, but I’m sure you can come up with better ideas. You can find the code here. I hope this little thing keeps you entertained for a while. It kept me entertained while I was trying to open up terminals after terminals with my earbuds. Have fun!
kaangiray26so you're telling me that I can get 3 extra keys if I give away 3 of my keys? Sounds right to me.