Questions about using Input.keyMapper

I was experimenting with using these 2 Parallel Map Event Pages to map the "i" key to open the background for an Inventory menu.

Inventory 1.pngInventory 2.png

This works fine. When you press "i" the image is either Shown or Erased.

But I have a couple of questions.

If I map a bunch of keys this way at the beginning of my game, will they persist? Meaning I will always be able to run a parallel event to see if they are being triggered?

Or is this specific to the map that the Event is on? Or is this only until the game is reloaded like after a save? Or is there some other limitation?

Essentially I would like to know if manually mapping these keys will remain valid in the game or if they really need to be added by a plugin.

Second question. Assuming the above method of mapping keys persists in the game, are there any problems with doing this? Or having a Parallel Event running to constantly check for those keys to be triggered and then do something? Like call Common Events.

Thanks for any clarification that you can provide!
you can either test it or set it inside a plugin as those are always readed,
you can call them anytime.

so if you Input.keyMapper[73] = "i"
you can always call it from there or find a keymapper and it should
work for all keys that way.
I'm pretty sure they are persistent across maps, I used that before and mapped all my keys at once, which worked across all maps. (If I recall correctly 😀 )

I don't know if they are persistent through saves, most likely they won't since the game engine would initialise the Input object, erasing your updates.
Or, you would have to do this through a plugin.
Thanks for the responses!

I just tested it by reloading a save and the "i" key no longer functions. So it doesn't persist after loading a save file.

It persists if I just exit to the Title screen and then Continue. But if I exit the game and then play test again and continue, it doesn't persist.

I will have to look for a key mapping plugin.
By default the input maps are initialised on game boot, then remain unchanged. So as you've seen, any changes made will persist for the rest of the play session, i.e. until you close the game.

Like ShadowDragon said, to make this into a plugin, you can simply put the code you have into its own .js file. That way, it'll apply itself when the game boots up. E.g.
JavaScript:
/*:
 * @target MZ
 * @plugindesc Add custom keyboard controls.
 * @url https://forums.rpgmakerweb.com/threads/174276/
 * @help Free to use and/or modify for any project, no credit required.
 */
Input.keyMapper[73] = 'i';
The header is optional, mostly for display purposes. @target MZ suppresses the "may not be written for MZ" warning in the Plugin Manager, which can be helpful to avoid hiding legitimate warnings from other plugins.
Thanks for the responses!

I just tested it by reloading a save and the "i" key no longer functions. So it doesn't persist after loading a save file.

It persists if I just exit to the Title screen and then Continue. But if I exit the game and then play test again and continue, it doesn't persist.

I will have to look for a key mapping plugin.
You could just write your own plugin using that same line of code and load it by patching it into the Input key Mapper object if you have basic needs for using specific keys. It will load it along with the rest of the keys.

Edit: probably best to not create a const key outside of scope haha. Removed it from the code.

JavaScript:
Input.keyMapper[73] = 'i';
Input.keyMapper[74] = 'j';
...

Edit: ninja'd by caethyril. 9999 damage. Super effective.
@caethyril and @bass2yang Thanks for that! I was wondering if a plugin for this could be that simple and didn’t require knowing something else to reference.

I will experiment with this tomorrow to see what happens.

Thanks again!
@caethyril and @bass2yang Thanks for that! I was wondering if a plugin for this could be that simple and didn’t require knowing something else to reference.

I will experiment with this tomorrow to see what happens.

Thanks again!
To hopefully help clarify this further: a plugin basically works like a large Script command that gets ran right before the game starts. So if you want to make some sort of a permanent change the engine, and want it to always happen, it's probably best to put that change into a plugin.
Thank you so much for this assistance @caethyril @bass2yang @Mac15001900 A couple more things I am curious about if anyone can answer.

I have a keyboard plugin by @ATT_Turan from last Advent (I think) that I also use and it works great. I took a look at it because I thought that would need to map all of the keys on the keyboard so you could type. I can see parts of what it is doing, but that plugin doesn't make it so the keyboard keys are accessible outside of something like Name Input Processing. I was just wondering why that might be? Maybe that plugin was made to solely work in specific circumstances and it isn't mapping all those keys in general?

Also, is there any issue with mapping a bunch a keys with a new plugin solely for that? Like making this plugin map 30-40 keys? Many games use hotkeys to do things and I am thinking about experimenting with those mechanics.

Of course, the more keys you have mapped, I guess the larger the Parallel Event would need to be to check all the Conditional Branches to see if any are being triggered. Or maybe you would need to have multiple Parallel Events running to check them all without experiencing any sort of delay or performance impact?

Anyway, if there isn't any real issue with mapping a bunch of keys this way, I will make the plugin later today and do some testing.

Thanks again!
I have a keyboard plugin by @ATT_Turan from last Advent (I think) that I also use and it works great.
...
Maybe that plugin was made to solely work in specific circumstances and it isn't mapping all those keys in general?
TUR_KeyboardEntry, from here?
Yes, it looks like it just ignores the keymapper completely in the situations for which is it designed, i.e.
- Use keyboard for entering names and numbers

Also, is there any issue with mapping a bunch a keys with a new plugin solely for that? Like making this plugin map 30-40 keys?
I don't imagine there would be any problems based purely on quantity, no. Just be aware which buttons already have an assigned response:
Spoiler: core keymapper
JavaScript:
Input.keyMapper = {
    9: "tab", // tab
    13: "ok", // enter
    16: "shift", // shift
    17: "control", // control
    18: "control", // alt
    27: "escape", // escape
    32: "ok", // space
    33: "pageup", // pageup
    34: "pagedown", // pagedown
    37: "left", // left arrow
    38: "up", // up arrow
    39: "right", // right arrow
    40: "down", // down arrow
    45: "escape", // insert
    81: "pageup", // Q
    87: "pagedown", // W
    88: "escape", // X
    90: "ok", // Z
    96: "escape", // numpad 0
    98: "down", // numpad 2
    100: "left", // numpad 4
    102: "right", // numpad 6
    104: "up", // numpad 8
    120: "debug" // F9
};
A button can't have two names assigned to it, e.g. Input.keyMapper[90] = "z"; would prevent the Z key from acting in its usual role as an alternative "ok" input. 👋

(Also, remember that the keyMapper is only for keyboard input, not gamepad or touch.)

Or maybe you would need to have multiple Parallel Events running to check them all without experiencing any sort of delay or performance impact?
😅 No, use 1 Parallel event for all the checks! RPG Maker processes its events in series: having multiple Parallels for the "same thing" like you describe is a waste of memory.
@caethyril Thank you so much for the additional clarification. Here is what the final plugin looks like.

Spoiler: Custom Keys Plugin
/*:
* @target MZ
* @plugindesc Add custom keyboard controls.
* @url https://forums.rpgmakerweb.com/threads/174276
* @Help Free to use and/or modify for any project, no credit required. Help from caethyril and plugin compiled by ZombieKidzRule.
*/
Input.keyMapper[48] = "0";
Input.keyMapper[49] = "1";
Input.keyMapper[50] = "2";
Input.keyMapper[51] = "3";
Input.keyMapper[52] = "4";
Input.keyMapper[53] = "5";
Input.keyMapper[54] = "6";
Input.keyMapper[55] = "7";
Input.keyMapper[56] = "8";
Input.keyMapper[57] = "9";
Input.keyMapper[65] = "a";
Input.keyMapper[66] = "b";
Input.keyMapper[67] = "c";
Input.keyMapper[68] = "d";
Input.keyMapper[69] = "e";
Input.keyMapper[70] = "f";
Input.keyMapper[71] = "g";
Input.keyMapper[72] = "h";
Input.keyMapper[73] = "i";
Input.keyMapper[74] = "j";
Input.keyMapper[75] = "k";
Input.keyMapper[76] = "l";
Input.keyMapper[77] = "m";
Input.keyMapper[78] = "n";
Input.keyMapper[79] = "o";
Input.keyMapper[80] = "p";
Input.keyMapper[82] = "r";
Input.keyMapper[83] = "s";
Input.keyMapper[84] = "t";
Input.keyMapper[85] = "u";
Input.keyMapper[86] = "v";
Input.keyMapper[89] = "y";

I am getting ready to test it now and see if it also recognizes the letter if the CAPS lock is on or if the SHIFT key is also being pressed for a capital letter.
Wait, am I misunderstanding something here? I mapped a key with script Input.KeyMapper and it does persist after closing and reopening and loading from a save, should it not be saving that info?
Are you only mapping it once or do you have an Event that maps it repeatedly?

When I tested it, the mapping persisted until I completely exited the game. Not just exiting to the title screen.

Once I closed the game and then restarted and continued from my save, the key was no longer mapped.

But I did see a tutorial where the person had a Common Event that was a parallel event that mapped the key and the parallel event just kept running.

Not sure what else you might be experiencing. You don’t have any plugins that might also map it?
I map it in the event that initializes a bunch of stuff in a cutscene at the start of my game. Yeah I pressed the X and closed out completely and then launched the distro build again. I also don't have any plugins that mess with key mapping, unless I missed something in DungeonMind Item Categories.
I am getting ready to test it now and see if it also recognizes the letter if the CAPS lock is on or if the SHIFT key is also being pressed for a capital letter.
No: shift & capslock are their own keys. E.g. the core mapper assigns the shift key (code 16) to the name "shift". That's the internal name for the dash button.

Each key on the keyboard has a unique code. This is the code by which the keyMapper is indexed. When a key is pressed (e.g. Z), the game checks its keyMapper for the code of that key (90). If present, it then flags the associated "button name" ("ok") as being in a "pressed" state.

The rest of the engine uses these "button name states" to decide how to respond to user input. So if a key is pressed but its code isn't listed in the mapper, the game will effectively ignore it.

Also, just a note for the plugin header: you can use @author to specify who made it, e.g.
JavaScript:
/*:
* @target MZ
* @plugindesc Add custom keyboard controls.
* @author ZombieKidzRule (with help from caethyril)
* @url https://forums.rpgmakerweb.com/threads/174276
* @Help Free to use and/or modify for any project, no credit required.
*/
Again, it's just display info, you can keep it in the help section if you prefer~


should it not be saving that info?
Well. The core scripts shouldn't, because they have no code for it. But key-mapping plugins certainly should, otherwise you'd have to reconfigure the controls every time you launch the game! 😅
I also don't have any plugins that mess with key mapping
To be sure, you could try turning off all plugins, save your project to apply Plugin Manager changes, then test. That should prevent it from happening. You could then turn plugins back on, one by one or in small groups, to try to narrow down the culprit. (Or just ignore it, if it's not causing problems.)
No: shift & capslock are their own keys. E.g. the core mapper assigns the shift key (code 16) to the name "shift". That's the internal name for the dash button.

Each key on the keyboard has a unique code. This is the code by which the keyMapper is indexed. When a key is pressed (e.g. Z), the game checks its keyMapper for the code of that key (90). If present, it then flags the associated "button name" ("ok") as being in a "pressed" state.

The rest of the engine uses these "button name states" to decide how to respond to user input. So if a key is pressed but its code isn't listed in the mapper, the game will effectively ignore it.
Bonus question: is the key index also depending of the keyboard layout (qwertz, qwerty, azerty, ...)? => Eg. 'z' is 90 on qwerty keyboards, but something else on qwertz keyboards, right?
Bonus question: is the key index also depending of the keyboard layout (qwertz, qwerty, azerty, ...)?
I looked into this a bit deeper and it seems MZ, like MV, still uses the old keyCode property:
Unfortunately, yes, that is implementation-dependent.
So I updated and tested the Custom Key plugin. I added a few more keys and I verified that having the CAPS lock on doesn't change the functionality. So if the CAPS lock is on, it still reads the key as if the CAPS lock was off.

I tried to map the minus and equals keys, but they didn't work. Perhaps the website that I used for the Key Codes uses a different number for those keys than what MZ recognizes? I used:

www.toptal.com/developers/keycode

I tested every key multiple times, including after a Save, Exit, and Restart and everything worked great.

Here is the text of the updated plugin if it helps anyone else. I can't believe it was this simple.

Spoiler: Custom Key Plugin Text
/*:
* @target MZ
* @plugindesc Add custom keyboard controls.
* @Author ZombieKidzRule (with help from caethyril)
* @url https://forums.rpgmakerweb.com/threads/174276
* @Help Free to use and/or modify for any project, no credit required.
*/
Input.keyMapper[48] = "0";
Input.keyMapper[49] = "1";
Input.keyMapper[50] = "2";
Input.keyMapper[51] = "3";
Input.keyMapper[52] = "4";
Input.keyMapper[53] = "5";
Input.keyMapper[54] = "6";
Input.keyMapper[55] = "7";
Input.keyMapper[56] = "8";
Input.keyMapper[57] = "9";
Input.keyMapper[65] = "a";
Input.keyMapper[66] = "b";
Input.keyMapper[67] = "c";
Input.keyMapper[68] = "d";
Input.keyMapper[69] = "e";
Input.keyMapper[70] = "f";
Input.keyMapper[71] = "g";
Input.keyMapper[72] = "h";
Input.keyMapper[73] = "i";
Input.keyMapper[74] = "j";
Input.keyMapper[75] = "k";
Input.keyMapper[76] = "l";
Input.keyMapper[77] = "m";
Input.keyMapper[78] = "n";
Input.keyMapper[79] = "o";
Input.keyMapper[80] = "p";
Input.keyMapper[82] = "r";
Input.keyMapper[83] = "s";
Input.keyMapper[84] = "t";
Input.keyMapper[85] = "u";
Input.keyMapper[86] = "v";
Input.keyMapper[89] = "y";
Input.keyMapper[219] = "[";
Input.keyMapper[221] = "]";

I also tested a Parallel Event that simulates using designated keys to show and erase images but to make it so if an image was already showing, none of the other keys could be activated to show another image.

This was just a quick test to simulate using keys to say open and close menus/windows. Like the M key opens the Map, or the I key opens the Inventory, or the C key opens the Character Sheet.

If anyone knows an easier, more efficient way to achieve the same results I would be very happy to learn how to do it!

The first screenshot is missing the Label: Fighter command line that is at the very beginning of the Event. I just noticed my snippet missed that starting line.

Spoiler: Parallel Event Activate Only 1 Key At A Time
CustomKey Fighter.pngCustomKey Paladin.pngCustomKey Ranger.pngCustomKey Cleric.png

I hope this helps someone who is looking for something similar!

Thanks again to everyone for helping me learn something new!
/*:
* @target MZ
* @plugindesc Add custom keyboard controls.
* @Author ZombieKidzRule (with help from caethyril)
* @url https://forums.rpgmakerweb.com/threads/174276
* @Help Free to use and/or modify for any project, no credit required.
*/
Try this - for kicks. Lol. (Untested)

Edit: I'm guessing the equal key code is either 61 or 187. You can put that in separately to test.

JavaScript:
(($) => {
    const str  = (x) => String.fromCharCode(x);
    for (let i = 48; i < 58; i++) $[i] = str(i);
    for (let j = 65; j < 90; j++) $[j] = str(j).toLowerCase();
    for (const k of [219, 221]  ) $[k] = str(k - 128);
    $[187] = '=';
    $[189] = '-';
})(Input.keyMapper);

Edit: And one more for variation because I like for-of and I just realized you had some keys not included:
JavaScript:
(($) => {
    const key = '0123456789ABCDEFGHIJKLMNOPRSTUVY=-[]';
    const val = (n) =>
        ['[',']'].includes(n) ? 128 :
                     n == '=' ? 126 :
                     n == '-' ? 144 : 0;
    for (const x of key) {
        const y = key.charCodeAt(key.indexOf(x)) + val(x);
        $[y]    = x.toLowerCase();
    }
})(Input.keyMapper);
@bass2yang I tried 61 for = and 173 for - based on the website I included above.

What is the code above supposed to be doing? I only recognize a couple of things from it.

If it is just to get to use the - and/or = key, those keys really aren't that important to me. I was just sort of curious that they didn't work like the rest of the keys I mapped.

Original thread: https://forums.rpgmakerweb.com/threads/questions-about-using-input-keymapper.174276/