mp.input: allow clients to reliably override keybinds#17418
mp.input: allow clients to reliably override keybinds#17418kasper93 merged 4 commits intompv-player:masterfrom
Conversation
|
Using undocumented internal functions may be questionable. But I wonder if this problem may be an indication that this particular function should be documented after all. |
guidocella
left a comment
There was a problem hiding this comment.
Ah so that is what was happening. In the original incarnation of select.lua I just defined keybindings with a timeout (https://github.com/guidocella/mpv-console-select/blob/7712c7cba1bf0b7dfa8ddd567ac56bf18c3f11fb/console-select.lua#L114).
Makes sense though this delays opened by 0.077 seconds half the time in my tests.
|
Also with this we can probably replace the |
Yes, but it's not quite as easy as it seems. When local function define_scroll_override_section()
local section = "mp_input_override_" .. mp.get_script_name()
-- I just derived this formatting from defaults.lua, not certain this is ideal (but does work in my testing).
local cfg = o.key_scroll_up .. " nonscalable script-binding " .. mp.get_script_name() ..
"/__forced_" .. o.key_scroll_up .. "\n" ..
o.key_scroll_down .. " nonscalable script-binding " .. mp.get_script_name() ..
"/__forced_" .. o.key_scroll_down
mp.input_define_section(section, cfg, "force")
return section
end
local function filter_bindings()
local section = define_scroll_override_section()
input.get({
prompt = "Filter bindings:",
opened = function ()
-- This is necessary to close the console if the oneshot
-- display_timer expires without typing anything.
searched_text = ""
-- Must be re-bound to override the console.lua bindings.
mp.input_enable_section(section, "allow-hide-cursor+allow-vo-dragging")
end,
edited = function (text)
reset_scroll_offsets()
searched_text = text:lower()
print_page(curr_page)
if display_timer.oneshot then
display_timer:kill()
display_timer:resume()
end
end,
closed = function ()
mp.input_disable_section(section)
searched_text = nil
if display_timer:is_enabled() then
print_page(curr_page)
if display_timer.oneshot then
display_timer:kill()
display_timer:resume()
end
end
end,
})
endIdeally there would be a proper API provided to implement this, something like |
Upon further reflection, we can do this relatively simply by unbinding all of the other key bindings: local function filter_bindings()
input.get({
prompt = "Filter bindings:",
opened = function ()
-- This is necessary to close the console if the oneshot
-- display_timer expires without typing anything.
searched_text = ""
-- Must be re-bound to override the console.lua bindings.
remove_page_bindings()
bind_scroll()
end,
edited = function (text)
reset_scroll_offsets()
searched_text = text:lower()
print_page(curr_page)
if display_timer.oneshot then
display_timer:kill()
display_timer:resume()
end
end,
closed = function ()
searched_text = nil
if display_timer:is_enabled() then
add_page_bindings()
print_page(curr_page)
if display_timer.oneshot then
display_timer:kill()
display_timer:resume()
end
end
end,
})
end |
|
Yeah that works. |
opened events
Yea, I used a timeout as well in the script I linked in the description, but I think it makes more sense to delay the |
|
The delay seems very random and variable anyway. Now I'm getting 20ms or < 1 ms. How about:
|
|
Um nevermind calling |
This commit uses the undocumented `mp.flush_keybindings()` method to ensure that all `console.lua` keybinds are set before sending the `opened` event to `mp.input` clients. Previously, the keybinds would only be flushed when `console.lua` went idle. As the `opened` event is sent before this happens, clients processing the event may end up flushing their own keybinds before this happens. This makes it unreliable to use the `opened` event to override keybinds set by `console.lua`; for example, adding different behaviour to `input.select` when using `Shift+Enter` instead of `Enter`. This commit allows the `opened` callback to be used to safely override `console.lua` keybinds. .luacheckrc: move flush_keybindings to globals
This commit uses the opened event to ensure that the page scroll keys (`UP`/`DOWN` by default) override the mp.input bindings, without overriding any other mp.input keys. This is much safer than the previous method, which required an undocumented option to be passed to `mp.input`, and was hardcoded to the default scroll keys.
This undocumented option was used by `stats.lua` to prevent the page scroll keys from being overridden by `console.lua`. The previous commits have removed the need for this hack, so it is now safe to remove.
315eb06 to
37f0261
Compare
Now cites overriding keybinds as the most notable use of the `opened` event.
37f0261 to
6069977
Compare
|
I have added this use-case to the documentation, but I haven't mentioned the input section issue, I figure that might derail the discussion somewhat. |
One of the interesting things that can be done with
mp.input.select()is to override the default keybinds. This allows scripts to extend what can be done when a select window is open. For example, I have a youtube-search script which usesinput.select()to display search results that the user can open. I want to provide an additionalShift+Enterkeybind to append an entry to the playlist instead of replacing the whole playlist, but this conflicts with thenewlinekeyboard input used byconsole.lua.To have this work, I need to register my
Shift+Enterkeybind afterconsole.lua. The intuitive way to do this is to add the keybinds during theopenedcallback, as this is only run afterconsole.luahas opened and activated its keybinds. However, for optimisation reasons,defaults.luaactually buffers new keybind registrations and flushes them only once the script goes idle. As theopenedevent is sent before this happens, clients processing the event may end up flushing their own keybinds beforeconsole.luadoes. This makes it unreliable to use theopenedevent to override keybinds set byconsole.luaThis PR uses the undocumented
mp.flush_keybindings()method to ensure that allconsole.luakeybinds are set before sending theopenedevent tomp.inputclients, which allows theopenedcallback to be used to safely overrideconsole.luakeybinds.The PR also updates
stats.luato use this new method to override the UP/DOWN keys and removes the previous hack that allowed this to work.