mpv icon indicating copy to clipboard operation
mpv copied to clipboard

[Windows] Accept Global Media Key Controls

Open mastermach50 opened this issue 3 years ago • 6 comments

How can I make mpv accept media key controls (play/pause/next/prev) globally? I did see solutions for this on mac and linux but not for windows, not anything that feels native at least.

I currently have an AutoHotKey script to do this but it has its limitations and problems.

I am fine with recompiling mpv for this

mastermach50 avatar Oct 15 '22 15:10 mastermach50

What kind of limitations and problems?

Here is another issue with AutoHotKey code example:

https://github.com/mpv-player/mpv/issues/9336

In mpv.net a global hotkey feature exist, it's based RegisterHotkey, so has limitations as well.

stax76 avatar Oct 16 '22 07:10 stax76

What kind of limitations and problems?

In AutoHotKey I am currently using ControlSend to send inputs to mpv without activating its window (similar to your script), but I am using an if statement to make sure that either mpv or the player currently active gets the play/pause instruction so for proper usability.

Relevant sections in my script:

; Play/Pause Media (mpv compatible)
!`::
    IfWinNotActive, ahk_exe mpv.exe
        ControlSend, ahk_parent, {Space}, ahk_exe mpv.exe
    Else
        SendInput, {Media_Play_Pause}
    Return
 
; Play Next (mpv compatible)
!+n::
    IfWinNotActive, ahk_exe mpv.exe
        ControlSend, ahk_parent, >, ahk_exe mpv.exe
    Else
        SendInput, {Media_Next}
    Return

; Play Previous (mpv compatible)
!+p::
    IfWinNotActive, ahk_exe mpv.exe
        ControlSend, ahk_parent, <, ahk_exe mpv.exe
    Else
        SendInput, {Media_Prev}
    Return

Problems:

  • This does not send input when mpv is on a different desktop than the one I am using on windows.
  • Since I cannot send Media_Play_Pause instructions if I ever change the bindings in mpv I will have to change it here also.
  • It is not a good thing for a media player to not support receiving Play/Pause instructions globally.

mastermach50 avatar Oct 16 '22 13:10 mastermach50

You can try sending commands using JSON IPC:

https://mpv.io/manual/master/#command-prompt-example

To hide the cmd window, you can use my run-hidden tool:

https://github.com/stax76/run-hidden

It's generally not a feature easy to do, on Windows there is RegisterHotkey which does not cover everything, and a powerful hook API which I would not use as it can make the system unstable when used incorrectly, additionally there is a special WM_APPCOMMAND API for media keys, it's complicated, AutoHotKey deals with all this very nicely and stable, so it's complicated, on Linux maybe even more.

stax76 avatar Oct 16 '22 13:10 stax76

So I did hear about mpv.net for windows, it seems fine but what I need is a lightweight player to play some songs while I am playing, so it needs to use as less ram as possible.

Will mpv.net be better in this case?

mastermach50 avatar Oct 19 '22 16:10 mastermach50

mpv is created with C which is more memory efficient than the C# of mpv.net. You can use the task manager if you want to see the difference.

stax76 avatar Oct 20 '22 13:10 stax76

Ok, will do. Thanks for your help

mastermach50 avatar Oct 20 '22 15:10 mastermach50

works for me after i changed mpv.exe to mpv.com

BESHOI avatar Jan 10 '23 11:01 BESHOI

I recently noticed that the next and previous some times miss with the speed of track instead of next and previous, so i tweaked it a little bit gist link.

BESHOI avatar Jan 12 '23 09:01 BESHOI