The Mission: Give the blue ThinkVantage button on my Thinkpad something usful to do
The Solution: Make it pause Spotify
The Implementation:
This one comes in two parts, first of all we need to look at what makes the Blue ThinkVantage button tick. Now, personally I dont get the point of that ThinkVantage menu dealio that comes up when you hit the blue button, so when I installed Windows 7 it wasnt something that got re-installed. However, to get the button funcational at all you need to install the HotKey Manager (avaliable on the Lenovo site, I dont have a link im afraid as I've had it installed forever to get the volume keys working. If you volume keys work, your all set, if they dont, you have to go find a driver before you continue I'm afraid) and then fire up The Registry.
Aww hell, I've always wanted to say this.
Warning: Playing with your registry can severly screw up your computer, children, life and any family pets. Registry hacking is the start of a long slippery slope down to Heroin addiction and prostitution. Or....some kind of disclaimer like that, basically - don't mess around in the registry, you'll probally brick your computer unless you know what you are doing.
So, we have totally ignored that warning and gone start/run/regedit and then navigated to HKLM/Software/IBM/TPHOTKEY. Now, you may, or may not have the following key, so if you don't have it, create it - you want to go into the 8001 key, which apparently is the blue button. Then you add a string value called "File" and give it the path to the program you want to fire off of the buton, so if you put "C:\\windows\\notepad.exe" (and it's a good idea to do that once to make sure it's working ok) when you hit the blue button, notepad will fire up. Note the double backslashes in the path, and remember to use the old ~1 DOS style folder naming, so C:\Program Files beecomes C:\\Progra~1\\.
http://forum.notebookreview.com/showthread.php?s=409a0bf7627cccc6ee612fc3efe18838&t=171644
Ok, thats part one. Now on to part B!
You need to write some code here, basically what we are after is a form which loads, finds and then fires a message to spotify telling it to play/pause, and then closes again. The following will work (sorry, its late I ant be bothered to code format this one):
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace PlayPause
{
public partial class Form1 : Form
{
private const int APPCOMMAND_PLAY_PAUSE = 14;
private const int WM_APPCOMMAND = 0x0319;
private System.Int32 iHandle;
[DllImport("user32.dll")]
public static extern IntPtr SendMessageW(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
[DllImport("User32.dll")]
public static extern int FindWindow(string strClassName,
string strWindowName);
[DllImport("User32.dll")]
public static extern Int32 SendMessage(
int hWnd, // handle to destination window
int Msg, // message
int wParam, // first message parameter
int lParam); // second message parameter
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
iHandle = FindWindow("SpotifyMainWindow", null);
int vCallReturn = SendMessage(iHandle, WM_APPCOMMAND, 0x00000000, APPCOMMAND_PLAY_PAUSE << 16);
this.Close();
}
catch (Exception)
{
this.Close();
}
}
}
}
What your doing is pulling in the methods you need from the Windows API, setting up your constants into integers, then on load you are using FindWindow and the Spotify main windows Class (you set the last parameter to blank because Spotify keeps changing it's title so you cant rely on that to identify the window) to get the window handle of Spotify, then sending the window a message of class WM_APPCOMMAND (The media keys commands) and the command itself, APPCOMMAND_PLAY_PAUSE (which we bitshift 16 places). We then close the window. I also have it set up to load minimised and not appear on the taskbar, so it dosnt look like a window has loaded at all.
You then tie the two steps toegether by pointing your registry key at the exe you just made and wam, the Blue Button is pausing Spotify!
Here are the links I used to figure the above out - I really dont get the oppotunity to play with Native code much, being a web developer, so it was a fun explore of the Interop Services.
http://msdn.microsoft.com/en-us/library/ms646275%28VS.85%29.aspx
http://www.codeproject.com/KB/cs/wmp_pinvoke.aspx?msg=735041
http://www.microsoft.com/indonesia/msdn/pinvoke.aspx
http://www.codeproject.com/KB/wtl/WTLAppButtons.aspx?msg=2332697
http://kalshagar.wikispaces.com/Remote+controlling+Windows+media+player+in+C
Currently rated 5.0 by 1 people
- Currently 5/5 Stars.
- 1
- 2
- 3
- 4
- 5