From 627e917c7f5671eb967ee04d3336aac5c3abb967 Mon Sep 17 00:00:00 2001 From: CogentRedTester Date: Wed, 18 Feb 2026 18:36:18 +1030 Subject: [PATCH 1/4] console.lua: flush keybinds before sending mp.input `opened` events 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 --- .luacheckrc | 2 +- player/lua/console.lua | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/.luacheckrc b/.luacheckrc index dda48b031d23b..d096c1c368391 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -57,6 +57,7 @@ local mp_globals = { disable_key_bindings = {}, enable_key_bindings = {}, find_config_file = {}, + flush_keybindings = {}, format_time = {}, get_mouse_pos = {}, set_key_bindings = {}, @@ -78,7 +79,6 @@ local mp_internal = { UNKNOWN_TYPE = { fields = { info = {}, type = {} }}, _legacy_overlay = { fields = { res_x = {}, res_y = {}, data = {}, update = {} }}, cancel_timer = {}, - flush_keybindings = {}, get_osd_margins = {}, input_define_section = {}, input_disable_section = {}, diff --git a/player/lua/console.lua b/player/lua/console.lua index d2bf222acb40e..406d83038e8f6 100644 --- a/player/lua/console.lua +++ b/player/lua/console.lua @@ -1736,6 +1736,10 @@ mp.register_script_message("get-input", function (args) end set_active(true) + + -- We want to ensure the keybindings have been set before sending the "opened" event + -- in case scripts want to override our bindings. + mp.flush_keybindings() mp.commandv("script-message-to", input_caller, input_caller_handler, "opened") end) From e92ba8d38520e95e57f0022b829826cb108d705a Mon Sep 17 00:00:00 2001 From: CogentRedTester Date: Thu, 19 Feb 2026 10:09:58 +1030 Subject: [PATCH 2/4] stats.lua: correctly override mp.input keybinds 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. --- player/lua/stats.lua | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/player/lua/stats.lua b/player/lua/stats.lua index dcb78501cb93a..408b5706c4e15 100644 --- a/player/lua/stats.lua +++ b/player/lua/stats.lua @@ -1579,6 +1579,9 @@ local function unbind_scroll() end end +local add_page_bindings +local remove_page_bindings + local function filter_bindings() input.get({ prompt = "Filter bindings:", @@ -1586,6 +1589,10 @@ local function filter_bindings() -- 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() @@ -1599,6 +1606,7 @@ local function filter_bindings() 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() @@ -1606,7 +1614,6 @@ local function filter_bindings() end end end, - dont_bind_up_down = true, }) end @@ -1648,7 +1655,7 @@ local function update_scroll_bindings(k) end -- Add keybindings for every page -local function add_page_bindings() +add_page_bindings = function() local function a(k) return function() reset_scroll_offsets() @@ -1667,7 +1674,7 @@ end -- Remove keybindings for every page -local function remove_page_bindings() +remove_page_bindings = function() for k, _ in pairs(pages) do mp.remove_key_binding("__forced_"..k) end From 0ecbb3eaf7a147a5c407d295157c9700085895f3 Mon Sep 17 00:00:00 2001 From: CogentRedTester Date: Thu, 19 Feb 2026 10:10:32 +1030 Subject: [PATCH 3/4] console.lua: remove undocumented `dont_bind_up_down` option 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. --- player/lua/console.lua | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/player/lua/console.lua b/player/lua/console.lua index 406d83038e8f6..cd753db122250 100644 --- a/player/lua/console.lua +++ b/player/lua/console.lua @@ -93,7 +93,6 @@ local MAX_LOG_LINES = 10000 local log_buffers = {} local log_offset = 0 local key_bindings = {} -local dont_bind_up_down = false local global_margins = { t = 0, b = 0 } local input_caller local input_caller_handler @@ -1573,12 +1572,10 @@ local function define_key_bindings() return end for _, bind in ipairs(get_bindings()) do - if not (dont_bind_up_down and (bind[1] == "up" or bind[1] == "down")) then - -- Generate arbitrary name for removing the bindings later. - local name = "_console_" .. (#key_bindings + 1) - key_bindings[#key_bindings + 1] = name - mp.add_forced_key_binding(bind[1], name, bind[2], {repeatable = true}) - end + -- Generate arbitrary name for removing the bindings later. + local name = "_console_" .. (#key_bindings + 1) + key_bindings[#key_bindings + 1] = name + mp.add_forced_key_binding(bind[1], name, bind[2], {repeatable = true}) end mp.add_forced_key_binding("any_unicode", "_console_text", text_input, {repeatable = true, complex = true}) @@ -1683,7 +1680,6 @@ mp.register_script_message("get-input", function (args) keep_open = args.keep_open default_item = args.default_item has_completions = args.has_completions - dont_bind_up_down = args.dont_bind_up_down searching_history = false if args.items then From 6069977be8b7f9b9cd3fa8eea6c6e11e8d43b183 Mon Sep 17 00:00:00 2001 From: CogentRedTester Date: Fri, 20 Feb 2026 14:10:02 +1030 Subject: [PATCH 4/4] DOCS/man/lua: update recommended use of the mp.input `opened` event Now cites overriding keybinds as the most notable use of the `opened` event. --- DOCS/man/lua.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DOCS/man/lua.rst b/DOCS/man/lua.rst index 2ce63ce671653..631ffd97c822c 100644 --- a/DOCS/man/lua.rst +++ b/DOCS/man/lua.rst @@ -938,7 +938,7 @@ REPL. ``opened`` A callback invoked when the console is shown. This can be used to - present a list of options with ``input.set_log()``. + override keybinds set by the console with ``mp.add_forced_key_binding()``. ``edited`` A callback invoked when the text changes. The first argument is the text