Recently I’ve started working on my desktop PC again, after it spent several years gathering dust and being tripped over. Before its extended rest, it had begun to exhibit symptoms suggesting the power supply was on death’s door and I had neither the need nor the inclination to fix it at the time.
During its hiatus, I’ve become accustomed to having the ability to skip songs at my fingertips using media keys, something afforded by pretty much any laptop released in the past decade. Unfortunately, the Logitech K200 I use with my desktop doesn’t provide, at least what I consider, a complete set of media keys:
- Volume up
- Volume down
- Web browser
- E-mail client
- Power off
Next and previous track buttons; not included.
I probably should have just replaced the keyboard at this point, but aside from this basic inadequacy, I quite like it. It’s well made; it doesn’t feel flimsy or weak. It’s satisfying to work on; the keys have a pleasant tactile feel. You can even rinse it under the tap after inadvertently sharing your morning coffee with it (unplug it first).
Now I’ve justified my determination to address the K200’s shortcomings, we’ll move on. Fortunately, all hope is not lost for resolving the issue with software alone, as while Logitech didn’t deem it necessary to provide next or previous track buttons, they did provide a few media keys that are a worthy sacrifice to our cause; browser and e-mail.
Changing What The Keys Do
Given we do have some sacrificial keys, we just need to change what happens when they are pressed. After a few Google searches I stumbled across a question by Maarx on Super User where he’s working down a similar path with a Dell keyboard. In his question, he provides a few examples of media keys successfully bound to different actions. His examples also show that the action that a media key performs when it’s pressed is stored in the Windows Registry under the key:
Each media key has its own key defined inside
AppKey which is named according
to the numeric value of the media key.
I managed to find a comprehensive list of the names and values of the media keys that Windows supports in Microsoft’s Windows documentation. The list is quite long, so I’m only going to include the media keys that are relevant for this post:
|APPCOMMAND_MEDIA_PLAY_PAUSE||14||Play or pause playback|
|APPCOMMAND_VOLUME_MUTE||8||Mute the volume|
|APPCOMMAND_VOLUME_UP||10||Raise the volume|
|APPCOMMAND_VOLUME_DOWN||9||Lower the volume|
|APPCOMMAND_MEDIA_PREVIOUSTRACK||12||Go to previous track|
|APPCOMMAND_MEDIA_NEXTTRACK||11||Go to next track|
In his question Maarx also mentioned that you can add a
ShellExecute entry to a key and the command included will be executed when the
relevant media key is pressed.
Knowing that, we can create a script for each action, which can then be executed
ShellExecute. All the script needs to do is emulate a key press for the
media key that our keyboard doesn’t have. Before writing our scripts we need to
know what the virtual-key code is for the media key we want to emulate.
Microsoft’s Windows documentation has a comprehensive list
of virtual-key codes. I’ve picked out the the relevant entries:
|VK_MEDIA_NEXT_TRACK||0xB0||Next Track key|
|VK_MEDIA_PREV_TRACK||0xB1||Previous Track key|
Now that we know where in the registry we need to make a change, we need to
create two scripts to be executed when the media key we want to use for each
function is pressed. As we’re working on Windows, a simple VB script seems a
good choice. I created the following scripts and saved them as
' Next Track ' Create an instance of WScript.Shell Set WshShell = CreateObject("WScript.Shell") ' Send the character code for APPCOMMAND_MEDIA_NEXTTRACK ' &H indicates that the virtual key code is in hexdecimal format WshShell.SendKeys chr(&HB0)
' Previous Track ' Create an instance of WScript.Shell Set WshShell = CreateObject("WScript.Shell") ' Send the character code for APPCOMMAND_MEDIA_PREVIOUSTRACK ' &H indicates that the virtual key code is in hexdecimal format WshShell.SendKeys chr(&HB1)
I placed my scripts in
C:\Windows\System32 for ease.
Editing The Registry
The last thing we need to do is make the required changes in the registry, to enable the appropriate script to be executed for our desired media keys. We’ll use Registry Editor for this.
Open Registry Editor from the start menu or enter
regedit in to a run prompt
Win + R). Navigate to where we need to make our changes as discussed earlier:
I found that the
AppKey key held a number of empty keys already:
I selected key 15 (corresponding to
APPCOMMAND_LAUNCH_MAIL) and added a new
String Value (
Edit > New > String Value)
ShellExecute. You can edit the value by selecting the new String Value
and selecting Modify… (
Edit > Modify...).
I set the value to
NextTrack.vbs which worked fine as I placed my scripts in
System32 directory. If you placed your scripts somewhere else, you would
need to enter a path to your script e.g.
Repeat the same process for key 7 (corresponding to
using the previous track script.
Now we have working next and previous track keys. I have noticed that they don’t appear to work while Registry Editor is in focus, so I’m not sure if it’s possible for an application to override the keys in the registry. Other than that, they seem to work perfectly.