Skip to content

Commit

Permalink
defaults.lua: handle canceled key bindings
Browse files Browse the repository at this point in the history
There is a subtle behavior difference for built-in/input.conf key bindings
and key bindings registered by scripts: when a key binding is canceled
(e.g. a mouse button is bound to a command, is pressed down, and then
another key is pressed which is bound to another command), the command is
not invoked if the binding is built-in/input.conf, but is invoked if it's
registered by scripts, because it's handled with a different mechanism,
which gives no way for scripts to detect this.

Fix this by using the newly available canceled flag to detect this.
If a key binding is canceled, the callback is now not invoked unless
the key binding is registered with the complex option. In this situation,
the callback is invoked with the canceled state available so that scripts
can detect and handle this situation.
  • Loading branch information
na-na-hi committed Jun 6, 2024
1 parent 2eff29c commit 0374d3e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
2 changes: 2 additions & 0 deletions DOCS/interface-changes/keybind-cancel.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
change `mp.add_key_binding` so that by default, the callback is not invoked if the event is canceled; clients should now use the `complex` option to detect this situation
add `canceled` entry to `mp.add_key_binding` callback argument
9 changes: 8 additions & 1 deletion DOCS/man/lua.rst
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,9 @@ The ``mp`` module is preloaded, although it can be loaded manually with

After calling this function, key presses will cause the function ``fn`` to
be called (unless the user remapped the key with another binding).
However, if the key binding is canceled (e.g. the key is logically released
but not physically released), the function will not be called, unless
``complex`` flag is set to ``true``.

The ``name`` argument should be a short symbolic string. It allows the user
to remap the key binding via input.conf using the ``script-message``
Expand Down Expand Up @@ -321,7 +324,11 @@ The ``mp`` module is preloaded, although it can be loaded manually with
tracked).

``is_mouse``
Boolean Whether the event was caused by a mouse button.
Boolean: Whether the event was caused by a mouse button.

``canceled``
Boolean: Whether the event was canceled.
Not all types of cancellations set this flag.

``key_name``
The name of they key that triggered this, or ``nil`` if
Expand Down
4 changes: 3 additions & 1 deletion player/lua/defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ local function add_binding(attrs, key, name, fn, rp)
fn({
event = key_states[state:sub(1, 1)] or "unknown",
is_mouse = state:sub(2, 2) == "m",
canceled = state:sub(3, 3) == "c",
key_name = key_name,
key_text = key_text,
})
Expand All @@ -222,7 +223,8 @@ local function add_binding(attrs, key, name, fn, rp)
-- Also, key repeat triggers the binding again.
local event = state:sub(1, 1)
local is_mouse = state:sub(2, 2) == "m"
if event == "r" and not repeatable then
local canceled = state:sub(3, 3) == "c"
if canceled or event == "r" and not repeatable then
return
end
if is_mouse and (event == "u" or event == "p") then
Expand Down

0 comments on commit 0374d3e

Please sign in to comment.