Can I send keystrokes to a specific application?

4

2

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?

Nick T

Posted 2012-01-15T21:55:32.410

Reputation: 2 417

hmm. On a Windows I am not sure, as my main computer is a mac. But I would imagine the solutions would be similar for mac and windows. Anyway, 3rd party apps sound like a really good idea but mac also has that built-in services menu in System Preferences>Keyboard>Services, so that you can specify the hotkey or keys, which application to use them for, and what they will do. – FALL3N – 2012-01-15T22:07:28.560

2The user mentions Auto Hotkey, so he's on Windows. – Daniel Beck – 2012-01-15T22:11:35.380

1

@Nick, I solved a similar problem using AutoHotkey: http://superuser.com/questions/368633/play-pause-pandora-com-with-a-media-key/371141#371141

– iglvzx – 2012-01-16T05:43:11.477

@iglvzx, that's a great answer. You should post the link as an answer here. – Dave Neeley – 2012-01-18T07:32:19.523

@Nick, I will take a shot at this tomorrow. There are a few ways to do this with AutoHotkey. – iglvzx – 2012-01-18T07:45:50.533

Answers

1

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

iglvzx

Posted 2012-01-15T21:55:32.410

Reputation: 21 611

1

The official (and easiest) way to send keys to a specific window is to use the ControlSend function:

ControlSend,, P            , Raphsody

ControlSend,, {Media_Stop} , ahk_id %HWND%

ControlSend,, ^{Space}     , ahk_pid %RAHP_PID%

etc.

Synetech

Posted 2012-01-15T21:55:32.410

Reputation: 63 242

0

I needed similar thing to do so out of curiosity I tryed both:

#Media_Play_Pause::
    WinGet, original, ID, A
    WinActivate, MyWindowTitle name - Rapsody
    Send ^p
    WinActivate, ahk_id %original%
return

and

Media_Play_Pause::
    ControlSend ahk_parent, ^p, MyWindowTitle name - Rapsody
return

It's probably obvious that second one not only is less complex but is also much faster.

Saulius

Posted 2012-01-15T21:55:32.410

Reputation: 103