LOVE python
I’ve done some more experimenting with Python lately, and I have to say it is even cooler than I had previously thought. Wow. Ok, here’s what happened:
Recently my keyboard has been sucking bad. The spacebar especially, would only work about 1/2 of the time. The rest of the time it would stick. Yuck. So I got a new keyboard, and found one I’m really happy with.
Also for a while there has been a variation in the backslash/pipe key on a keyboard. Sure, most people very rarely use these keys. As a developer, though, I use them all the time. So keyboards with an <enter> key twice as tall as normal, a backspace key half the width as normal, and a tiny little backslash/pipe key next to that… well those keyboards are bad.
I’ve also noticed that many keyboards now forgoe the <insert> key for a twice-as-tall <delete> key. What’s up with that?
What’s different about this one is that is has the ‘media keys,’ which were previously only available on the non-standard (aka ‘weird’) keyboards. This was the first standard-layout keyboard with the media keys. Cool. Not only that but it’s somewhat configurable. You can set which application the media keys will control. In fact, if you go into the .ini file, you can actually see the WM_COMMAND message numbers being sent to these applications.
The only problem is, I usually listen to music from my laptop, where my headphones are plugged in. My main computer has good speakers, but they don’t have a headphone jack, so switching between speakers and headphones would mean pulling my computer out and rearranging the wires. Not cool. Hmmmmm…. what could be done about this?
The answer: write a couple of Python scripts. A sender for my main computer, and a receiver for my laptop. I use the configuration capability of the media keyboard to hook the keyboard up to my Python script. I use the WM_COMMAND message numbers from the .ini file in the receiving end to control the media player on the laptop. I even made them nice little system-tray icons. The sender side lets you choose if you want to control the local version or the remote version.
It wasn’t the easiest thing because it used a lot of aspects about Python I wasn’t familiar with. I used the win32gui, win32con, and win32api functions to intercept the WndProc (to receive the original media keyboard messages), and use SendMessage to send the messages on the receiving end to the media player. I also used threading and sockets (as well as the SocketServer module) for the first time. A lot of learning curve, which is why it was a big fat 24 hours before I was finished.
24 hours? What? That’s not a long time, you say. That’s also on top of other work projects. It basically amounted to the free time I had in a day.
That’s how cool Python is.