MZ Where/how to add custom button commands?

So I have a script that does a thing. The script works, and is currently executed in a common event called from a testing event. I want to move it to a button/key command on the map scene. I've found the input key map, that part's not the problem. I've found a few scenes in the base code that essentially listen for a keypress, uses a bind function. I don't really understand how binding works and how to use it, but I can probably get it to work.

The problem is that I'm not sure where to PUT my code. The map scene doesn't seem to have anything for handling inputs other than some stuff for the menu button, and even that seems to only involve touch input.

Currently I intend to use two buttons already in the input table, as long as they don't already do something on the map screen. But I need to FIND where that stuff is defined/handled. Any help would be appreciated! Thanks in advance!
There are two ways to approach this.

Way 1 is to have a parallel process running on the map. You can do Conditional Branch -> Tab 4 -> Button to detect key presses, then execute your common event.

The other is to use a plugin that allows you to directly set a given key to call a specific common event. MV had Yanfly's Button Common Events, MZ must have an equivalent by now.
I'd prefer to avoid a parallel event setup, then I'd have to make sure it's running on EVERY map, and ONLY on the maps, I don't need it running during menus and such. The feature involved is going to be semi-integral to the gameplay, so it needs to be available on every map. Technically it's just a shortcut for something you can already do, but given how often a player may need to do it, I'd like to minimize how many times a player needs to dip into the menu for something absurdly simple when a quick key press can do the same thing. Less menu-navigating, more playing.

I'd also like to make sure I keep my use of parallel processes to an absolute minimum. I have no idea at this stage how much is going to go into the game, and I don't believe that this feature is going to need such a process, when the code totals about a dozen lines and is intended to be triggered just like moving on the map is, via a dedicated pair of buttons.

As for the plugin... I'm trying to write it myself to AVOID having to get a plugin for just this. For such a small thing, it'd be relatively trivial for me to implement it myself. My issue is finding WHERE in the code I need to put my functions. The rest is either done or just waiting on function placement.
Then just search the game code for the Input.keymapper table and see which functions reference it and call other methods based on its value.

I'd probably add it to _onKeyDown(), but there might be a lower level method that's more appropriate.
Hi there!

If you want that to only work on scene map, you can do these two steps:

1 - Go to the Input.KeyMapper and define your button there.

Input.keyMapper["button code"] = "buttonName"

You can find the right key code here:

The button name can be any you want, just make sure to not make any duplicate.

2 - On scene map update function, you can add the following, somewhere on the update function:

JavaScript:
if(Input.isTriggered("buttonName"){
Call your common event
}

You can replace "is triggered" with other input method of you want:
is pressed, etc...

I whoud probably also take a look on how they implemented the menu button for learning purposes.
Hi there!

If you want that to only work on scene map, you can do these two steps:

1 - Go to the Input.KeyMapper and define your button there.

Input.keyMapper["button code"] = "buttonName"

You can find the right key code here:

The button name can be any you want, just make sure to not make any duplicate.

2 - On scene map update function, you can add the following, somewhere on the update function:

JavaScript:
if(Input.isTriggered("buttonName"){
Call your common event
}

You can replace "is triggered" with other input method of you want:
is pressed, etc...

I whoud probably also take a look on how they implemented the menu button for learning purposes.
The key mapper was the first thing I'd found, that's how I knew there were two unused buttons to test with.

Then just search the game code for the Input.keymapper table and see which functions reference it and call other methods based on its value.

I'd probably add it to _onKeyDown(), but there might be a lower level method that's more appropriate.
Searching for keymapper stuff was literally the FIRST thing I tried, after finding said mapper.

I actually found where I needed my code. There's an object for the player that handles movement on the map. Since that's where the relevant key detection for movement runs, and my mechanic is only usable when one can freely move ANYWAY, the Game_Player's update function was the perfect place to put it. I found it not because of the keymapper, but because of the Input.isTriggered function.

TL;DR: It was in an obscure object that I didn't know existed. My thing works now.

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/where-how-to-add-custom-button-commands.145689/