AutoHotkey is one of those utilities that I am very grateful that I found. It has made my desktop working experience all the better - if you don't know what I am talking about check out my intro to AutoHotkey. Today I am going to go over a set of hot keys that I really like and use daily. The hot keys to control iTunes, the nice thing is that they work no matter where you're at on your computer and no matter what application your are in. I found the basic implementation of these over at the AutoHotkey forums.
The basic idea is that we will be able to do the following: open, minimize, start, stop, go to next, and go to previous. Now, the main idea here is that with AutoHotkey we can send commands to our iTunes application no matter if it is minimized or not. In order to achieve this we are going to use the AutoHotkey command ControlSend. This command simulates sending keystrokes to a control or window.
Looking at the documentation for this there are 6 parameters. Following is taken directly from the documentation page.
| Control | Can be either ClassNN (the classname and instance number of the control) or the name/text of the control, both of which can be determined via Window Spy. When using name/text, the matching behavior is determined by SetTitleMatchMode. If this parameter is blank or omitted, the target window's topmost control will be used. If this parameter is ahk_parent, the keystrokes will be sent directly to the control's parent window (see Automating Winamp for an example). To operate upon a control's HWND (window handle), leave the Control parameter blank and specify ahk_id %ControlHwnd% for the WinTitle parameter (this also works on hidden controls even when DetectHiddenWindows is Off) . The HWND of a control is typically retrieved via ControlGet Hwnd, MouseGetPos, or DllCall. |
| Keys | The sequence of keys to send (see the Send command for details). To send a literal comma, escape it (`,). The rate at which characters are sent is determined by SetKeyDelay. Unlike the Send command, mouse clicks cannot be sent by ControlSend. Use ControlClick for that. |
| WinTitle | The title or partial title of the target window (the matching behavior is determined by SetTitleMatchMode). If this and the next 3 parameters are omitted, the Last Found Window will be used. If this is the letter A and the next 3 parameters are omitted, the active window will be used. To use a window class, specify ahk_class ExactClassName (shown by Window Spy). To use a process identifier (PID), specify ahk_pid %VarContainingPID%. To use a window group, specify ahk_group GroupName. To use a window's unique ID number, specify ahk_id %VarContainingID%. The search can be narrowed by specifying multiple criteria. For example: My File.txt ahk_class Notepad |
| WinText | If present, this parameter must be a substring from a single text element of the target window (as revealed by the included Window Spy utility). Hidden text elements are detected if DetectHiddenText is ON. |
| ExcludeTitle | Windows whose titles include this value will not be considered. |
| ExcludeText | Windows whose text include this value will not be considered. |
The only ones we care about are the first three though. These define the control to send the keystokes to, the keystrokes themselves, and finally the window of the control. For the first we pass ahk_parent (telling it to use the direct parent control of our window). The second we are going to pass the keystroke which varies for the different commands. For the third parameter we pass iTunes ahk_class iTunes which is the title iTunes and then the class name. The title and class can be gotten by using the window spy application (AU3_Spy.exe) that comes with AutoHotkey, just look in your AutoHotkey installation folder (C:\Program Files\AutoHotkey). Using this application you can click on windows and get their class and title, not to mention lots of other useful information.
So, without anymore delay here is the entire script piece that can be added to your current AutoHotkey script.
; win+right -> next song
; win+left -> previous song
; win+space -> play/pause
#right::
DetectHiddenWindows , On
ControlSend , ahk_parent, ^{right}, iTunes ahk_class iTunes
DetectHiddenWindows , Off
return
#left::
DetectHiddenWindows , On
ControlSend , ahk_parent, ^{left}, iTunes ahk_class iTunes
DetectHiddenWindows , Off
return
#space::
DetectHiddenWindows , On
ControlSend , ahk_parent, {space}, iTunes ahk_class iTunes
DetectHiddenWindows , Off
return
#z::
DetectHiddenWindows , On
IfWinActive , iTunes ahk_class iTunes
WinMinimize
else
Run %ProgramFiles%\iTunes\iTunes.exe
DetectHiddenWindows , Off
return
Now above you can see that in the first three hotkeys we are just running the ControlSend command. Wrapped around the command is enabling and then disabling detection of hidden windows. This makes sure that if iTunes is minimized it will send the keystrokes to it. The last hotkey acts a little different. It will open or minimize/restore iTunes, checking first if it is running. You will also notice that all the hotkeys are bound to #key, which is the Windows key plus any other key - for pause/play it is windows + space.
That pretty much wraps it up. I hope someone out there found something useful and learned something. If anyone has any questions about AutoHotkey or anything else feel free to leave a comment. You can also download the script we created today. Also I ran across this small little application that does basically what we did today if anyone is simply looking for the end solution.
01/04/2009 - 21:53
I was inspired by your use of AutoHotKey to solve another minor problem I have:
Implementing multiple iTunes libraries to keep each of my family members libraries seperate using one login account. The idea is to keep the master song lists seperate so we don’t have to weed through 100’s of songs that are not of interest by each other.
This can easily be done by holding down the shift key when starting iTunes, but that is a hassle to remember when docking the iPod or starting iTunes so I have autohotkey detect the startup of itunes.exe and hold the shift key for 5 seconds for us! See below for sample script:
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
#InstallKeybdHook
SetKeyDelay, 20
SetTimer, KeepRunning
return
;waits for iTunes to run, holds down the shift key for 5 seconds then waits for itunes to close to resume waiting for iTunes to launch again.
;This will cause iTunes to ask for a library on startup so we can keep our master song lists separate.
KeepRunning:
Process, wait, itunes.exe
{
send {shift down}
sleep 5000
send {shift up}
Process, waitclose, itunes.exe
}
return
Enjoy! (just wrote it tonight so minimal testing has been done with it).
01/21/2009 - 02:37
Thanks for this guys.. been looking for something similar!
04/07/2009 - 05:33
I found a program that has a really nice hot key function, they're user selectable and it offers a lot of features. I've tried playing around with making my own hot key program and i've even tried a few, but for the price, this program appears to be solid. Check it out, www.skinitunes.com
08/21/2009 - 09:49
Fantastic! Thanks for posting a great solution that was simple to implement.
11/19/2009 - 22:15
this is just wat i wanted!!! thanks so much!
Add Comment
[language] [/language]
Examples:
[javascript] [/javascript]
[actionscript] [/actionscript]
[csharp] [/csharp]
See here for supported languages.
Javascript must be enabled to submit anonymous comments - or you can login.