MV Detecting a Keypress in Javascript

Hello, I want to make a piano interface in RPGMaker, by making it run a common event in parralel, triggered by a switch. The second keyboard row (ASDF) is used for the piano, and the cancel logical event is just routine code to return back to the player's position.

This is my current code:

const keyMap = {
'A': 0, 'S': 1, 'D': 2, 'F': 3, 'G': 4,
'H': 5, 'J': 6, 'K': 7, 'L': 8, ';': 9, "'": 10
};

for (const key in keyMap) {
if (Input.isTriggered(key.toLowerCase())) {
const n = keyMap[key];
$gameVariables.setValue(51, n);
AudioManager.playSe({name: `hurazom_piano_note_${n}`, volume:90, pitch:100, pan:0});
}
}

if (Input.isTriggered('cancel')) {
$gameScreen.clearPictures();
$gameScreen.startTint([0, 0, 0, 0], 60);
$gameSwitches.setValue(43, false);
$gamePlayer.reserveTransfer(
$gameVariables.value(54),
$gameVariables.value(55),
$gameVariables.value(56),
$gameVariables.value(53),
2
);
}

It doesn't seem to work for me. Does anyone know how I can fix this?

Update: I manage to fix it. In the rpg_core.js, I updated Input.keyMapper to include definitions for the keys: F - L and ; as such:

70: 'f',
71: 'g',
72: 'h',
73: 'i',
74: 'j',
75: 'k',
76: 'l',
186: ';'
I made a piano in the evented way, I have no idea why you need that
many variables though.

you don't have to swap maps as you can set it to autorun and a parallel.

even though, I did make some improvements to it, but I will improve it more.
I did left the somewhere on 1 of the threads searching for one.

if I can't find that thread, I can share it here too, as it is really easy.
this doesn't listen specifically to triggers, but to both triggers and presses,
so you can play the note short or long (depending how long you hold the
key down.

ZE_KeyMapper is powerful enough to use the entire keyboard.
I made one which is a UnifiedBridge so keyboard and gamepad works.

depending what you target and easy to setup and dynamically change keys
or even extend it.

but if you want to see what it doesn't work, set console log inside of it:

JavaScript:
const keyMap = {
'A': 0, 'S': 1, 'D': 2, 'F': 3, 'G': 4,
'H': 5, 'J': 6, 'K': 7, 'L': 8, ';': 9, "'": 10
};

for (const key in keyMap) {
if (Input.isTriggered(key.toLowerCase())) {
    const n = keyMap[key];
    $gameVariables.setValue(51, n);
    console.log("var 51: ", $gameVariables.setValue(51, n)); //=> see what it prints
    AudioManager.playSe({name: `hurazom_piano_note_${n}`, volume:90, pitch:100, pan:0});
  }
}

if (Input.isTriggered('cancel')) {
$gameScreen.clearPictures();
$gameScreen.startTint([0, 0, 0, 0], 60);
$gameSwitches.setValue(43, false);
$gamePlayer.reserveTransfer(
$gameVariables.value(54),
$gameVariables.value(55),
$gameVariables.value(56),
$gameVariables.value(53),
2
);
    console.log("Scene is cancels")
}

if needed, you can print the value of the variables, see if any has an number.
how are those initialized? and where?
I made a piano in the evented way, I have no idea why you need that
many variables though.

you don't have to swap maps as you can set it to autorun and a parallel.

even though, I did make some improvements to it, but I will improve it more.
I did left the somewhere on 1 of the threads searching for one.

if I can't find that thread, I can share it here too, as it is really easy.
this doesn't listen specifically to triggers, but to both triggers and presses,
so you can play the note short or long (depending how long you hold the
key down.

ZE_KeyMapper is powerful enough to use the entire keyboard.
I made one which is a UnifiedBridge so keyboard and gamepad works.

depending what you target and easy to setup and dynamically change keys
or even extend it.

but if you want to see what it doesn't work, set console log inside of it:

JavaScript:
const keyMap = {
'A': 0, 'S': 1, 'D': 2, 'F': 3, 'G': 4,
'H': 5, 'J': 6, 'K': 7, 'L': 8, ';': 9, "'": 10
};

for (const key in keyMap) {
if (Input.isTriggered(key.toLowerCase())) {
    const n = keyMap[key];
    $gameVariables.setValue(51, n);
    console.log("var 51: ", $gameVariables.setValue(51, n)); //=> see what it prints
    AudioManager.playSe({name: `hurazom_piano_note_${n}`, volume:90, pitch:100, pan:0});
  }
}

if (Input.isTriggered('cancel')) {
$gameScreen.clearPictures();
$gameScreen.startTint([0, 0, 0, 0], 60);
$gameSwitches.setValue(43, false);
$gamePlayer.reserveTransfer(
$gameVariables.value(54),
$gameVariables.value(55),
$gameVariables.value(56),
$gameVariables.value(53),
2
);
    console.log("Scene is cancels")
}

if needed, you can print the value of the variables, see if any has an number.
how are those initialized? and where?
I managed to fix the problem.

The main problem was that the key codes were not defined, so for example: Input.isTriggered("f") would only register if I had the F key defined in the core Input mapper. But for the future, I will have a look at ZE_KeyMapper, for more generalised cases.

Original thread: https://forums.rpgmakerweb.com/threads/detecting-a-keypress-in-javascript.179277/