MZ Help with mapping keys and effects to mouse buttons

[EDIT: Ok, so the problem (at least one of them) is that the TouchInput.isCancelled() can't be used like that to detect a mouse right button press...but then how do I detect a right mouse click? I tried googling some javascript and it talks about a mouse event taking a value of 2 or something, but I can't make anything up with that, LOL.]

So I want the left mouse button to imitate pressing keyboard "c" and the right mouse button to imitate pressing keyboard "a" and also play an animation on the player. I googled old threads here and came up with this (no idea what I'm doing). It seems like the left button is working, but the right button only does its job once and then stops responding(?) If what I have tried to come up with by googling looks like sh*t, can someone suggest some easier solution? (I'm using RMMZ and playing around with the ARPG plugin).

Disable mouse movement and mouse menu access with scripts (seems to be working?):
Spoiler
Code:
Game_Temp.prototype.setDestination = function(x, y) {
};

Code:
Scene_Map.prototype.isMenuCalled = function() {
    return Input.isTriggered('menu');
};

Bind the keyboard keys to the mouse buttons (+ play animation on right click). Like I said above, there's a problem with the right button.

Spoiler
LEFT BUTTON
IF script: TouchInput.isPressed()

Code:
TouchInput._onLeftButtonDown = function(event) {
document.dispatchEvent(
new KeyboardEvent("keydown", {
key: "c",
keyCode: 67,
code: "c",
which: 67
})
);

setTimeout(() => {
document.dispatchEvent(new KeyboardEvent("keyup", {
key: "c",
keyCode: 67,
code: "c",
which: 67
}));
}, 250);
};

RIGHT BUTTON
IF script: TouchInput.isCancelled()

Code:
TouchInput._onRightButtonDown = function(event) {
document.dispatchEvent(
new KeyboardEvent("keydown", {
key: "a",
keyCode: 97,
code: "a",
which: 97
})
);

setTimeout(() => {
document.dispatchEvent(new KeyboardEvent("keyup", {
key: "a",
keyCode: 97,
code: "a",
which: 97
}));
}, 250);
};

$gameTemp.requestAnimation([$gamePlayer], 21);
I am very confused about what you could be trying to do by dispatching keyboard events from a mouse, but you definitely shouldnt be editing function prototypes from Event Script blocks. Those changes will remain in place.

IF you really want to do this I would suggest having the following two script calls, one at the start of your sequence:
Code:
TouchInput.__CustomSequenceListener = function (event) {
    // left button
    if (event.button === 0) {
        document.dispatchEvent(
          new KeyboardEvent("keydown", {
            key: "c",
            keyCode: 67,
            code: "c",
            which: 67
          })
        );
        
        setTimeout(() => {
          document.dispatchEvent(new KeyboardEvent("keyup", {
            key: "c",
            keyCode: 67,
            code: "c",
            which: 67
          }));
        }, 250);
    // right button
    } else if (event.button === 2) {
      TouchInput._onRightButtonDown = function(event) {
      document.dispatchEvent(
      new KeyboardEvent("keydown", {
      key: "a",
      keyCode: 97,
      code: "a",
      which: 97
      })
      );

      setTimeout(() => {
      document.dispatchEvent(new KeyboardEvent("keyup", {
      key: "a",
      keyCode: 97,
      code: "a",
      which: 97
      }));
      }, 250);
      };

      $gameTemp.requestAnimation([$gamePlayer], 21);
    }
};
document.addEventListener('mousedown', TouchInput.__CustomSequenceListener);

This adds a new event listener to the document for mousedown which has an "event" parmeter which will be 0 for left mouse, 1 for middle mouse, or 2 for right mouse.

and one after your sequence is over:
Code:
document.removeEventListener('mousedown', TouchInput.__CustomSequenceListener);
TouchInput.__CustomSequenceListener = undefined;
to clean them up after they are no longer needed.
Thanks! I just managed to get something working a while ago, but your method is probably more efficient. But even with your script calls, I still have to modify the mouse movement and menu access with those other script calls (at least it seems so?)

The reason I'm doing this is that I'm playing around with the ABS plugin and want to launch attacks and guard from the mouse buttons. I don't really know about coding, I'm just trying to come up with whatever, LOL. It's surprisingly hard for me, even with googling. And even if I get something working, I don't know what is necessary/unnecessary and what might affect something else.
Oh I see, its likely there's a cleaner way to modify that plugin then, but I'd have to see the code for it to actually help and it might be more complicated than the work around you have now. If it's just for prototyping ideas best to just go with whatever's the easiest and fastest anyway

Original thread: https://forums.rpgmakerweb.com/threads/help-with-mapping-keys-and-effects-to-mouse-buttons.156531/