Windows – send keystrokes to a specific application

autohotkeykeyboardmedia playerwindows 7

I have a multimedia keyboard with Play/Pause, Next, Previous, etc. buttons on it, and use Rhapsody for my music. There is an issue, however, where the program will not recognize (or OS will not pass) the multimedia keystrokes unless it is the active window.

Is there a way to get around this with a 3rd party program (Autohotkey or the like) and have it capture keystrokes and feed them to specific applications?

Best Answer

The easiest method to send keystrokes to an inactive window with AutoHotkey is to: (1) remember the current active window, (2) activate the target window, (3) send keystrokes, and (4) activate the original window.

This following script works with Rhapsody:

Media_Play_Pause::
WinGet, original, ID, A
WinActivate, Rhapsody
Send ^p
WinActivate, ahk_id %original%
Exit

Media_Stop::
WinGet, original, ID, A
WinActivate, Rhapsody
Send ^s
WinActivate, ahk_id %original%
Exit

Media_Prev::
WinGet, original, ID, A
WinActivate, Rhapsody
Send ^b
WinActivate, ahk_id %original%
Exit

Media_Next::
WinGet, original, ID, A
WinActivate, Rhapsody
Send ^f
WinActivate, ahk_id %original%
Exit

Note: If AutoHotkey does not register Media_Play_Pause et al to your keyboard's media keys, you will have to manually retrieve the scan codes. Instructions can be found on my answer here: https://superuser.com/a/371141/100787

Related Question