You need a combination of two things....
First is the correct title matching, which has been spelled out in the other answers (there are several nuances to be aware of). Note that certain things may not work as expected all the time, for example this code does not seem to always work...
#Persistent
F4::
if WinExist("ahk_exe vlc.exe")
WinClose
Return
This code above returns a valid ID from WinExist()
but trying to close it via WinClose
may not work in some cases (possibly because of how VLC handles the WM_CLOSE
window message)
This code on the hand may work more generally...
#Persistent
SetTitleMatchMode, 2
F4::WinClose, VLC
Second, besides proper title matching, you need an actual hotkey definition.
I don't know what hotkey you're hoping to use, but #+::
is two modifier keys (Windows key and Shift key), with no actual hotkey defined.
If you're not sure if the hotkey definition is working correctly, just put a MsgBox
statement and see if that triggers correctly...
For example:
#+::MsgBox ; This doesn't do anything
F4::MsgBox ; This works when pressing F4
Then once you have your hotkey definition working, you can figure out the rest of your code.
Also, if you are going to do multi-line testing, for example putting a MsgBox AND your code in a single block, start on the next line after the hotkey definition for your code, and make sure to use a Return
statement to close out the hotkey definition...
#Persistent
SetTitleMatchMode, 2
F4::
MsgBox % "Hotkey Triggered: " A_ThisHotkey "`n`n" . "About to close VLC..."
WinClose, VLC
Return
ahk_exe vlc.exe
. See autohotkey.com/docs/misc/WinTitle.htm