History

js: key bindings: ensure priorities for same-key bindings

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.

Avi Halachmi (:avih) committed 5 years ago

Commit 7f4841f1 parent 1d1d1fb1 file · +14 −4Browse files

Showing the changes to player/javascript (1 of 1 file). Show all

Unified · Split

player/javascript/defaults.js+14−4
@@ -276,9 +276,18 @@ function dispatch_key_binding(name, state, key_name) { · 275 unmodified lines
276 276
277var binds_tid = 0; // flush timer id. actual id's are always true-thy277var binds_tid = 0; // flush timer id. actual id's are always true-thy
278mp.flush_key_bindings = function flush_key_bindings() {278mp.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 var def = [], forced = [];284 var def = [], forced = [];
280 for (var n in binds) // Array.join() will later skip undefined .input285 for (var n in binds)
281 (binds[n].forced ? forced : def).push(binds[n].input);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 var sect = "input_" + mp.script_name;292 var sect = "input_" + mp.script_name;
284 mp.commandv("define-section", sect, def.join("\n"), "default");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 fn = name;315 fn = name;
307 name = false;316 name = false;
308 }317 }
309 if (!name)
310 name = "__keybinding" + next_bid++; // new unique binding name
311 var key_data = {forced: forced};318 var key_data = {forced: forced};
312 switch (typeof opts) { // merge opts into key_data319 switch (typeof opts) { // merge opts into key_data
313 case "string": key_data[opts] = true; break;320 case "string": key_data[opts] = true; break;
314 case "object": for (var o in opts) key_data[o] = opts[o];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 if (key_data.complex) {327 if (key_data.complex) {
318 mp.register_script_message(name, function msg_cb() {328 mp.register_script_message(name, function msg_cb() {

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