mpv icon indicating copy to clipboard operation
mpv copied to clipboard

Toggle console with a single key

Open Obegg opened this issue 3 years ago • 10 comments

Currently - to open console you have to press 2 keys, its not a toggle. Is there any way to make it show and hide console by only one key?

Obegg avatar Sep 06 '22 00:09 Obegg

mp.add_forced_key_binding("esc", nil, function()
	mp.commandv("script-binding", "console/enable")
end)

With that you can open and close the console with escape.

Copy that text into a .lua file in your scripts folder.

christoph-heinrich avatar Sep 06 '22 01:09 christoph-heinrich

^ and this wouldn't work in input.conf?

Hrxn avatar Sep 06 '22 01:09 Hrxn

^ and this wouldn't work in input.conf?

No, I tried it in input.conf first, but apparently it has to be a forced key bind to overwrite the escape key.

christoph-heinrich avatar Sep 06 '22 01:09 christoph-heinrich

But other keys work as expected here for the console toggle?

Hrxn avatar Sep 06 '22 01:09 Hrxn

The console uses escape for closing it, and you can't change that without changing the code. And so if the goal is toggling, and closing has to be escape, then opening also has to be escape.

christoph-heinrich avatar Sep 06 '22 01:09 christoph-heinrich

So complicated, for what? the stats (frame dropped and etc) is a toggle, why cant console be the same?

Obegg avatar Sep 06 '22 01:09 Obegg

mp.add_forced_key_binding("esc", nil, function()
	mp.commandv("script-binding", "console/enable")
end)

With that you can open and close the console with escape.

Copy that text into a .lua file in your scripts folder.

The thing is - I don't want to use escape key as the toggle, let's say I want to use g (example), it doesn't work.

Obegg avatar Sep 06 '22 02:09 Obegg

~Alternatively you make a PR to add a binding for disabling to the console, analogous to it's enable binding. Then you can use any key binding for toggling that does not get overwritten when console is open. You can find the key bindings that get enabled upon opening the console here.~

Edit: Actually that wouldn't work. I guess you could add a binding specifically for toggling.

christoph-heinrich avatar Sep 06 '22 02:09 christoph-heinrich

~~Not sure what the commotion is about, you can easily bind it to the key you want. Here's what I use:~~

F9 script-binding console/enable

Edit: nevermind

sfan5 avatar Sep 06 '22 07:09 sfan5

This was discussed in https://github.com/mpv-player/mpv/pull/10075#issuecomment-1097343116.

I thought that if the console enabled and disabled an input section instead of binding and unbinding the keybindings on open and close, you could add bindings like TAB {console} keypress ESC. But I later noticed that rossy's original script at https://github.com/rossy/mpv-repl/blob/master/repl.lua did use input sections, and wm4 removed them when merging it, which must be because he deprecated input sections, though with him gone most likely no one is going to remove them. So I'm hesitant about restoring them. And even with an input section you could only invoke other keypresses and not console.lua's functions directly, though they could be converted to script messages.

He actually forgot to remove the line mp.enable_key_bindings('console-input', 'allow-hide-cursor+allow-vo-dragging') so you can add {console-input} keybindings, as long as they're not the same as other console.lua keybindings since it uses add_forced_key_binding, but they are never disabled, e.g.

F1 script-binding console/enable
F1 {console-input} keypress esc

Pressing F1 twice toggles the console but pressing it again no longer enables it.

guidocella avatar Sep 06 '22 07:09 guidocella

because he deprecated input sections

what is the supposed replacement to these?

eugenesvk avatar Jan 17 '23 19:01 eugenesvk

There is no replacement, you should keep using input sections since the developer that wanted to remove them left.

guidocella avatar Jan 17 '23 19:01 guidocella

You can just use this script actually:

local open = false

mp.add_key_binding('F1', function ()
    mp.commandv('script-message-to', 'console', open and 'disable' or 'enable')
    open = not open
end)

guidocella avatar Apr 25 '24 18:04 guidocella

@guidocella Does this script example also work with

[input] Key ` is bound to:
[input] 1.  'script-binding console/enable' in <builtin>:1 (default)

(i.e. the standard console key), or does this break something else?

Edit

Here's what I meant:

local openconsole = false

mp.add_key_binding('`', function ()
    mp.commandv('script-message-to', 'console', openconsole and 'disable' or 'enable')
    openconsole = not openconsole
end)

Hrxn avatar Apr 25 '24 21:04 Hrxn

You can just use this script actually:

Since I use input-default-bindings=no, I need to add this to input.conf, but how?

Obegg avatar Apr 25 '24 21:04 Obegg

Nope it only works with keys not bound by console.lua.

Scripts go in .lua files in in the scripts directory, not in input.conf.

guidocella avatar Apr 25 '24 21:04 guidocella

Scripts go in .lua files

Sorry, I might have given you the wrong idea, I know .lua files go in scripts folder, I'm asking how I can call this function using input.conf? I did create a .lua file called console-toggle and I want to call it since I use input-default-bindings=no therefor any key from any script doesn't do anything.

Nvm, it's F1 script-binding console_toggle/__keybinding1

Obegg avatar Apr 25 '24 21:04 Obegg

Just give a name to the binding as the second argument

local open = false

mp.add_key_binding(nil, 'toggle-console', function ()
    mp.commandv('script-message-to', 'console', open and 'disable' or 'enable')
    open = not open
end)

and bind script-message toggle-console

guidocella avatar Apr 25 '24 21:04 guidocella

Finally I think I can close this issue, thank you!

Obegg avatar Apr 25 '24 22:04 Obegg