Previously, if a script bound the same key more than once to different functions (without removing the older bindings first), then pressing the key triggered any of the bound functions arbitrarily[1]. Now the newest binding for the same key is always the active one. If the newest binding is then removed - the second-newest will become active, etc. (same mechanism as before, except that the active key was not always the newest). [1] The order was implementation-defined, however `for (name in obj)' happens to iterate. With mujs that's currently alhabetic order, and name is from mp.add_[forced_]key_binding(key, name...) or generated internally if name is not provided. So a binding with higher alphabetic `name' value had priority over lower name value.
Showing the changes to player/javascript (1 of 1 file). Show all
Unified · Split
| @@ -276,9 +276,18 @@ function dispatch_key_binding(name, state, key_name) { · 275 unmodified lines | ||
| 276 | 276 | |
| 277 | 277 | var binds_tid = 0; // flush timer id. actual id's are always true-thy |
| 278 | 278 | mp.flush_key_bindings = function flush_key_bindings() { |
| 279 | + function prioritized_inputs(arr) { | |
| 280 | + return arr.sort(function(a, b) { return a.id > b.id }) | |
| 281 | + .map(function(bind) { return bind.input }); | |
| 282 | + } | |
| 283 | + | |
| 279 | 284 | var def = [], forced = []; |
| 280 | − for (var n in binds) // Array.join() will later skip undefined .input | |
| 281 | − (binds[n].forced ? forced : def).push(binds[n].input); | |
| 285 | + for (var n in binds) | |
| 286 | + if (binds[n].input) | |
| 287 | + (binds[n].forced ? forced : def).push(binds[n]); | |
| 288 | + // newer bindings for the same key override/hide older ones | |
| 289 | + def = prioritized_inputs(def); | |
| 290 | + forced = prioritized_inputs(forced); | |
| 282 | 291 | |
| 283 | 292 | var sect = "input_" + mp.script_name; |
| 284 | 293 | mp.commandv("define-section", sect, def.join("\n"), "default"); |
| @@ -306,13 +315,14 @@ function add_binding(forced, key, name, fn, opts) { · 21 unmodified lines | ||
| 306 | 315 | fn = name; |
| 307 | 316 | name = false; |
| 308 | 317 | } |
| 309 | − if (!name) | |
| 310 | − name = "__keybinding" + next_bid++; // new unique binding name | |
| 311 | 318 | var key_data = {forced: forced}; |
| 312 | 319 | switch (typeof opts) { // merge opts into key_data |
| 313 | 320 | case "string": key_data[opts] = true; break; |
| 314 | 321 | case "object": for (var o in opts) key_data[o] = opts[o]; |
| 315 | 322 | } |
| 323 | + key_data.id = next_bid++; | |
| 324 | + if (!name) | |
| 325 | + name = "__keybinding" + key_data.id; // new unique binding name | |
| 316 | 326 | |
| 317 | 327 | if (key_data.complex) { |
| 318 | 328 | mp.register_script_message(name, function msg_cb() { |