2

I want to setup a hotkey for closing vlc or windows media player using autohotkey. I came across this page in the guide- https://www.autohotkey.com/docs/commands/WinClose.htm

But it doesn't seem to work, here's what I tried:-

#+:: Winclose ,"VLC Media player"
#+:: Winclose ,"VLC Media player 2.0.1"
#+:: Winclose ,"vlc"

None of them seem to work. This is when nothing is playing in vlc. How do I setup a hotkey to close it with anything running in vlc? Or Windows Media Player?

Note: I know we can Send ^Q or Send !F4 but that only works when the vlc window is active, but I want to be able to end VLC when it is playing music in the background.

1

3 Answers 3

0

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
2

The problem are the quotes and perhaps the TitleMatchMode.

This will work:

SetTitleMatchMode, 2
#+:: Winclose, VLC

References:

1

None of them seem to work.

There are likely multiple problems:

  • There should be no quotes around the window title.

  • In your examples, you would want to use e.g. WinClose, VLC media player, where VLC media player is the exact title of the window without a file loaded.

  • The title of the window changes when playing media. It becomes e.g. Awesome Song.ext - VLC media player when playing a file called Awesome Song.ext. This can technically be worked around with SetTitleMatchMode, 2, which enables partial title matching (as presented in the other current answer to this question).

  • In testing, I couldn't get #! to work as a hotkey combination. So I might suggest picking another key or combination (I assume this is intended to be Win + Shift?).

I want to be able to end VLC when it is playing music in the background.

If you want a simple way to kill VLC while it has a file loaded (or is currently playing), I might suggest trying e.g.:

F4::
    if WinExist("ahk_exe vlc.exe")
        WinClose

My understanding is that omitting the parameters to WinClose uses the last found window from e.g. if WinExist(). See the documentation on WinTitle - Last Found Window.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.