简体   繁体   中英

How to hold a modifier key so ahk sends keypresses to a different window

I'm pretty new to ahk.

Say I have two separate vlc windows open and playing videos.

If one of the vlc windows is active, then I want to be able to hold a key to make all keypresses go to the other vlc window.

ie if I press Left , then it sends Left to the active vlc window, but if I hold ALT + Left , then it sends Left to the other vlc window.

If no vlc window is active, then I don't want autohotkey to do anything.

This is so I can control two vlc windows without having to click and choose which one is active.

I looked up GroupAdd hoping I could use a group that includes both vlc windows, but couldn't find a way to target specific vlc windows from the group to send keys to it.

edit: I got a very basic version working, but I feel it's pretty ugly. I would like a way to send any key that's pressed if you hold Shift to the other vlc window. Also this seems a bit unreliable in switching focus if you use it a few times quickly in succession.

GroupAdd, vlcgroup, ahk_exe vlc.exe
return

#IfWinActive ahk_exe vlc.exe
+Left::   ; shift-left
GroupActivate, vlcgroup, r
Send {Left}
GroupActivate, vlcgroup, r
return

A simple approach could be done like this:

#IfWinActive, ahk_exe vlc.exe
+Left::
    WinGet, WindowList, List, ahk_exe vlc.exe
    BottomMostVlcHwnd := WindowList%WindowList% 
    ControlSend, , {Left}, % "ahk_id " BottomMostVlcHwnd
return

^+Left::
    WinGet, WindowList, List, ahk_exe vlc.exe
    BottomMostVlcHwnd := WindowList%WindowList% 
    ControlSend, , ^{Left}, % "ahk_id " BottomMostVlcHwnd
return

...

#IfWinActive

You'd write out each hotkey.
WinGet, , List (docs) returns a legacy pseudo-array (docs) of hwnds to Vlc windows.
The last element in that array will be the bottom most window.
You can get the last element of a pseudo array via a dynamic variable trick WindowList%WindowList% .
Essentially you're accessing a variable named WindowListN , where N is the number for the last element in the pseudo-array.

Then ControlSend (docs) is used to send keys to the background window without the need to activate it.
And you refer to the background window by a window name like ahk_id 0x1234567 (docs) .


Of course writing out the hotkey for each key is pretty repetitive, so we can do something much better:

#IfWinActive, ahk_exe vlc.exe
+Left::
^+Left::
+Right::
^+Right::
+PgUp::
+PgDn::
    WinGet, WindowList, List, ahk_exe vlc.exe
    ControlSend, , % StrReplace(A_ThisHotkey, "+", "{") "}", % "ahk_id " WindowList%WindowList%
return
#IfWinActive

The hotkey definitions are just stacked on top of each other.
(If there were even more definitions, a loop with the Hotkey (docs) command could be nicer)

A_ThisHotkey (docs) will contain the hotkey that was used, and StrReplace (docs) is used to automatically replace the + with a { .
And finally the closing brace } is appended to the end.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM