Using a Microsoft Natural Ergonomic Keyboard 4000 to control MediaMonkey with Visual C# Sat, Jun 22. 2013
The challenge
My keyboard of choice at home is the Microsoft Natural Ergonomic Keyboard 4000 and it's a great keyboard for touch typists. However, I did have some problems using the special keys to control MediaMonkey, which is my media player of choice for music. Here I'll describe how I tackled that by making tiny executables for this purpose by means of Visual C# (Visual Studio Express 2012 for Desktop). I realize there are many ways to tackle this challenge and this is just one of them, but it's a way I found performs so well that I decided to dedicate a blog entry to it.
Of course I realize you don't need a Microsoft Natural Ergonomic Keyboard 4000 for this to work or even a Microsoft keyboard, as long as it is a keyboard with some extra keys (preferably media keys) and you can use some software to configure the keys to run an executable when pressed. In that case, the steps necessary to accomplish the same goal will of course differ somewhat.
First try: PowerShell
MediaMonkey makes it easy to be controlled from the outside by exposing a COM object called SongsDB. Initially I used PowerShell for MediaMonkey control, but it had the following disadvantages:
- Slow to start up: it took up to several seconds until MediaMonkey reacted to the press of a key
- It got in the way: because running PowerShell involved opening a command prompt, a command prompt window would pop up briefly. Using the start command was no option, because it is integrated into cmd.exe (there is no start.exe), and because PowerShell insists on running an executable instead of only a batch command like start (which of course makes sense, because start needs cmd.exe to even work).
- Crash and burn: if MediaMonkey wasn't already running when pressing one of the keys to control MediaMonkey, because its COM object was referenced, MediaMonkey would start automatically and then crash immediately after starting.
In the settings of the Microsoft Mouse and Keyboard Center, I had configured the following special keys to run the following commands:
- the Play / Pause key (open a program, webpage, or file):
- the Browser Forward key (open a program, webpage, or file):
- the Browser Back key (open a program, webpage, or file):
The better way: running a tiny executable
What I finally settled on was making an executable program by means of Visual C# (in Visual Studio Express 2012 for Desktop). The resulting executables are tiny (about 6 KiB), so they start up fast and the steps in Visual Studio and the C# code necessary to accomplish this, are not too bad either. Because I started a new project configured as a console application, which I later changed to Windows application (but without any code to generate a window), the executable conveniently runs on the background without any visible window at all.
I'll show here the steps necessary to generate the executables by starting a new project for every executable, each for a key used to control MediaMonkey.
- Start a new project in Visual Studio:
- Make sure it's a Console Application, give it a name something like MediaMonkeyKeyAction (and give the solution the same name; I've named mine MediaMonkeyPlayPause) while also making sure Create directory for solution is checked:
- Now, in the Solution Explorer pane on the right, right-click on the project and choose Properties:
- Change the output type to Windows Application (so no window will pop up):
- In the Solution Explorer pane on the right, right-click on the solution and choose Properties:
- Choose Configuration Properties on the left and change the Configuration column to Release:
- Again, in the Solution Explorer on the right, right-click on References (expanding the project if necessary) and choose Add Reference...:
- Expand COM on the left, choose Type Libraries, tick the checkbox to the left of MediaMonkey Library and click on the OK button (if for whatever reason MediaMonkey Library is not in the COM type libraries list, just choose Browse on the left, click the Browse... button, browse to the directory where you've installed MediaMonkey, select MediaMonkey.exe, click the Add button, make sure the checkbox to the left of MediaMonkey.exe is ticked and click the OK button):
- Type or paste the following code in the Program.cs editor window:
- Now, choose Build Solution in the Build menu (or press F7):
- The Build Output pane on the bottom should show something like this (if not, you've probably done something wrong):
- Repeat the above steps for MediaMonkeyPlayNext, only this time type/paste the following code in the Program.cs editor window:
- Then repeat the above steps for MediaMonkeyPlayPrevious, only this time type/paste the following code in the Program.cs editor window:
- You may have noticed that the resulting executables even start MediaMonkey for you if it isn't running when you press one of the MediaMonkey control keys!
- Now, copy the resulting executables to a directory of your choice (I've just made a new directory called C:\Program Files (x86)\MediaMonkeyControl), make sure the Play / Pause, Browser Forward and Browser Back keys are reconfigured to run these executables and presto, you're done!
- Finally, don't forget to have fun and think of me from time to time while feeling a warm, fuzzy feeling in your heart!