History

defaults.lua: support scalable mp.add_key_binding()

For complex key bindings, the table now contains a new member of the current key scale. mp.add_key_binding() now accepts the scalable flag to make the binding scalable.

nanahi committed 2 years ago

Commit e3e22de1 parent 34571e72 files · +18 −4Browse files

Unified · Split

2 files
  1. DOCS/man/lua.rst +11 −0
  2. player/lua/defaults.lua +7 −4
DOCS/man/lua.rst+11−0
@@ -323,6 +323,13 @@ The ``mp`` module is preloaded, although it can be loaded manually with · 322 unmodified lines
323 If set to ``true``, enables key repeat for this specific binding.323 If set to ``true``, enables key repeat for this specific binding.
324 This option only makes sense when ``complex`` is not set to ``true``.324 This option only makes sense when ``complex`` is not set to ``true``.
325 325
326 ``scalable``
327 If set to ``true``, enables key scaling for this specific binding.
328 This option only makes sense when ``complex`` is set to ``true``.
329 Note that this has no effect if the key binding is invoked by
330 ``script-binding`` command, where the scalability of the command
331 takes precedence.
332
326 ``complex``333 ``complex``
327 If set to ``true``, then ``fn`` is called on key down, repeat and up334 If set to ``true``, then ``fn`` is called on key down, repeat and up
328 events, with the first argument being a table. This table has the335 events, with the first argument being a table. This table has the
@@ -350,6 +357,10 @@ The ``mp`` module is preloaded, although it can be loaded manually with · 21 unmodified lines
350 description of ``script-binding`` command for details (this357 description of ``script-binding`` command for details (this
351 field is equivalent to the 5th argument).358 field is equivalent to the 5th argument).
352 359
360 ``scale``
361 The scale of the key, such as the ones produced by ``WHEEL_*``
362 keys. The scale is 1 if the key is nonscalable.
363
353 Internally, key bindings are dispatched via the ``script-message-to`` or364 Internally, key bindings are dispatched via the ``script-message-to`` or
354 ``script-binding`` input commands and ``mp.register_script_message``.365 ``script-binding`` input commands and ``mp.register_script_message``.
355 366
player/lua/defaults.lua+7−4
@@ -60,10 +60,10 @@ local function reserve_binding() · 59 unmodified lines
60 return "__keybinding" .. tostring(message_id)60 return "__keybinding" .. tostring(message_id)
61end61end
62 62
63local function dispatch_key_binding(name, state, key_name, key_text)63local function dispatch_key_binding(name, state, key_name, key_text, scale)
64 local fn = dispatch_key_bindings[name]64 local fn = dispatch_key_bindings[name]
65 if fn then65 if fn then
66 fn(name, state, key_name, key_text)66 fn(name, state, key_name, key_text, scale)
67 end67 end
68end68end
69 69
@@ -186,6 +186,7 @@ local function add_binding(attrs, key, name, fn, rp) · 116 unmodified lines
186 name = reserve_binding()186 name = reserve_binding()
187 end187 end
188 local repeatable = rp == "repeatable" or rp["repeatable"]188 local repeatable = rp == "repeatable" or rp["repeatable"]
189 local scalable = rp == "scalable" or rp["scalable"]
189 if rp["forced"] then190 if rp["forced"] then
190 attrs.forced = true191 attrs.forced = true
191 end192 end
@@ -200,7 +201,7 @@ local function add_binding(attrs, key, name, fn, rp) · 8 unmodified lines
200 ["r"] = "repeat",201 ["r"] = "repeat",
201 ["p"] = "press",202 ["p"] = "press",
202 }203 }
203 key_cb = function(_, state, key_name, key_text)204 key_cb = function(_, state, key_name, key_text, scale)
204 if key_text == "" then205 if key_text == "" then
205 key_text = nil206 key_text = nil
206 end207 end
@@ -210,6 +211,7 @@ local function add_binding(attrs, key, name, fn, rp) · 3 unmodified lines
210 canceled = state:sub(3, 3) == "c",211 canceled = state:sub(3, 3) == "c",
211 key_name = key_name,212 key_name = key_name,
212 key_text = key_text,213 key_text = key_text,
214 scale = tonumber(scale),
213 })215 })
214 end216 end
215 msg_cb = function()217 msg_cb = function()
@@ -235,8 +237,9 @@ local function add_binding(attrs, key, name, fn, rp) · 19 unmodified lines
235 end237 end
236 msg_cb = fn238 msg_cb = fn
237 end239 end
240 local prefix = scalable and "" or " nonscalable"
238 if key and #key > 0 then241 if key and #key > 0 then
239 attrs.bind = key .. " script-binding " .. mp.script_name .. "/" .. name242 attrs.bind = key .. prefix .. " script-binding " .. mp.script_name .. "/" .. name
240 end243 end
241 attrs.name = name244 attrs.name = name
242 -- new bindings override old ones (but do not overwrite them)245 -- new bindings override old ones (but do not overwrite them)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t