#registering multiple keybindings

1 messages · Page 1 of 1 (latest)

magic magnet
#

Let's continue here then

#

This seems to be the current code:

 for (const i in preconfigkeys) {
    console.log(`${i}${preconfigkeys[i].keymodifiers}`); 
    game.keybindings.register("token elevator", `elevation${i}`, {
      name: `Change token elevation by ${preconfigkeys[i].elevationchange}`,
      editable: [{
        key: "Backquote", 
        modifiers: preconfigkeys[i].keymodifiers
      }],
      onDown: () => {elevation(i);}
    })

I would do the following instead:

#
const preconfigkeys = [
  { elevationchange: -10, keymodifiers: ["Shift", "Control"] },
  { elevationchange: -5, keymodifiers: ["Control"] },
  { elevationchange: 0, keymodifiers: ["Shift", "Alt"] },
  { elevationchange: 5, keymodifiers: [] },
  { elevationchange: 10, keymodifiers: ["Shift"] },
];

for (const preconfigkey of preconfigkeys) {
  game.keybindings.register("token elevator", `elevation${preconfigkey.elevationchange}`, {
    name: `Change token elevation by ${preconfigkey.elevationchange}`,
    editable: [
      {
        key: "Backquote",
        modifiers: preconfigkey.keymodifiers,
      },
    ],
    onDown: () => {
      elevation(i);
    },
  });
}
#

also, module names probably shouldn't have spaces in them, so please use "token-elevator" instead of "token elevator".

#

also applies to the name in the module.json etc.

olive hazel
#

Hi, I am back. I will try your suggestion. Thanks for the hint on the module name, I did not look into these things yet, because at the moment this is still a worldscript that hooked into F-for-flatfooted. Using its own name puts it under General now.

#

"Uncaught SyntaxError: missing ) after argument list"

#

Ah, the hooks.on (

#

I was already trying around how to get this: keymodifiers: ["Shift", "Control"] },
and this together modifiers: preconfigkey.keymodifiers,

#

But at this point I only just learned that Shift+Control does not work and did not wrap my head around where which quotes have to be.

#

So this is a welcome simplification of the problem

#

Your solution also allows for the "5" case to be empty [], which does not throw an error, while the old `` solution did.

#

Next I will incorporate some suggestions by Varriount, including renaming the function to a verb instead of a noun.

olive hazel
magic magnet
#

it's not a string, it's an array

#

an empty array

#

I think I don't completely understand what you mean

olive hazel
#

Me neither, because I am not fluent in the correct terminology yet. I meant that the single array object uses literally with all its [] and "" and everything without it having to be put into quotes by itself. It's not a number and seemingly it's not a string, but it includes everything as if it were a string for the sake of including all characters.
All the examples I found when looking at "array of objects" either used quotes for anything with characters or use no quotes for numbers. So the keymodifiers: ["Shift", "Control"] format came as a pleasant surprise.

#

Currently reading up on where I should put semicolons and where I "might" not have/want to.

#

To clean this up. Also fixed the " elevation(i);" still being in there (with is invalid after removing the for i loop part).

magic magnet
#

an array is just a list of things. you can have an array of strings (["a", "b", "foobar"]), an array of numbers ([1, 2, 42]), or whatever else you want

olive hazel
#

Now that the modifiers part became so much easier to handle I code the reset to 0 part back in, turn it into a module and include localization.

#

The "whatever else you want" part including such combinations as ["xyz012"] was the surprising part.

#

And I assumed that internally that would still be a string of chars then.

magic magnet
#

you can also mix the stuff inside the array, e.g. [0, "foobar", false, {key: "value"}, ["another array inside"]]

olive hazel
#

The nice part about this is that this "whatever" is then literally forwarded to the "modifiers: preconfigkey.keymodifiers," line, which again I did not anticipate. Some hours ago I was contemplating that something akin to macro assember would be useful in this situation where the compiler inserts the literal part. But obviously the array already handles just that.

#

Nice stuff.

olive hazel
#

@pure crystal @tired kayak @magic magnet Thanks again for the hints and code snippets. The "Token Elevation" module is officially released and available in Foundry's installer now. The license is practically Public Domain.

magic magnet
#

Great!

#

(By „practically public domain“, you mean as close as possible to public domain as copy right allows in your legislation?)

olive hazel
#

It is also equivalent to the 0BSD (Zero-Clause BSD) license, but using the more "licenses for dummies" wording of MIT.

olive hazel
#

Less sleep than usual, though... zzzzZZ

pure crystal
#

Generally:

  • If you truly don't care what is done with your code, use MIT or BSD
  • If you want to prevent companies from taking your code, modifying it, then selling the modified code, use GPL. Though this makes people in general less likely to use it, especially if it's a library.