MZ Check if player is pressing any button on keyboard

There's actually a thread of it here, but it's not questioned nor answered properly, so I don't know where to go from there.

I simply need a script call to check if player is pressing any button on keyboard.
Not from mouse or gamepad.
Only any button from keyboard input.

Thank you~
You could try (e.g. in Conditional Branch -> Script):
JavaScript:
Object.values(Input.keyMapper).some(keyName => Input.isPressed(keyName))
That's a little inefficient regarding duplicate keys, but I think it ought to do the trick~
That's a little inefficient regarding duplicate keys, but I think it ought to do the trick~
i tried out the same thing, but i realized that it doesn't exclude inputs from gamepads. I wasnt sure how to make it be keyboard specific without changing Input._onKeyUp and down
@Robro33 - good point! Yea, _currentState is shared between keyboard & gamepad, so if it's vital to be able to distinguish those then some plugin would be needed to track the source(s) of each input.

Like you say, it looks like _onKeyDown & _onKeyUp would be sufficient for flagging keyboard inputs.
You could also check for input based on what was being pressed (use a switch that you can turn on and off during the game to check with the common event - change or add other code in the conditional branch as needed)

Edit: this will only check for the input keys you have defined - change the wait time as needed:

Spoiler
JavaScript:
Input.isPressed(Input._latestButton)
s1.png

That said, I do agree that an event listener would work best

Edit: this will listen for ANY key you press - you can add conditions inside the function with curly brackets {} if you have more than one line of code:
Spoiler
JavaScript:
onkeydown = (event) => console.log(`Key Pressed: ${event.key}`);
or
JavaScript:
addEventListener('keydown', event => console.log(`Key Pressed: ${event.key}`));
You should be able to erase the event after the code runs in parallel and it should persist until you change it afterward.
Spoiler
s2.png
Building on what @Robro33 suggested earlier, in case it helps (briefly tested):
Spoiler: plugin code
JavaScript:
/*:
 * @target MZ
 * @plugindesc Adds a function to check if any keyboard input is currently held.
 * @author Caethyril
 * @url https://forums.rpgmakerweb.com/threads/175466/
 * @help Adds a script call, e.g. for Conditional Branch:
 *      isKeyboardInputHeld()
 * Returns true or false.
 *
 * Free to use and/or modify for any project, no credit required.
 */
;void (() => {
"use strict";
  /** Private storage for {@linkcode isKeyboardInputHeld}. */
  const keysHeld = new Set();

  /** @returns {boolean} `true` iff a keyboard input is held. */
  const isKeyboardInputHeld = function() { return keysHeld.size > 0; };
  globalThis.isKeyboardInputHeld = isKeyboardInputHeld;  // to global

  /** @param {KeyboardEvent} event @returns {number} Corresponding `keyCode`. */
  const keyCode = function(event) { return event.keyCode; };

  // Patch - update `keysHeld` when appropriate.
  void (alias => {
    Input._onKeyDown = function(event) {
      alias.apply(this, arguments);
      const k = keyCode(event);
      if (k in this.keyMapper) {
        const name = this.keyMapper[k];
        if (name && this._currentState[name])
          keysHeld.add(k);
      }
    };
  })(Input._onKeyDown);

  // Patch - update `keysHeld` when appropriate.
  void (alias => {
    Input._onKeyUp = function(event) {
      alias.apply(this, arguments);
      const k = keyCode(event);
      if (k) {
        const name = this.keyMapper[k];
        if (name && !this._currentState[name])
          keysHeld.delete(k);
      }
    };
  })(Input._onKeyUp);

})();


@bass2yang - the core Input._latestButton implementation is not robust for that purpose: it is only designed for tracking the press time for the latest input received. If you hold, say, Shift, then press & release Enter, Shift is still held but _latestButton will be null (cf Input.update, rmmz_core).
@bass2yang - the core Input._latestButton implementation is not robust for that purpose: it is only designed for tracking the press time for the latest input received. If you hold, say, Shift, then press & release Enter, Shift is still held but _latestButton will be null (cf Input.update, rmmz_core).
Thanks! I wasn't even thinking about extra keys being held - I was just testing one key at a time.
Building on what @Robro33 suggested earlier, in case it helps (briefly tested):
Spoiler: plugin code
JavaScript:
/*:
 * @target MZ
 * @plugindesc Adds a function to check if any keyboard input is currently held.
 * @author Caethyril
 * @url https://forums.rpgmakerweb.com/threads/175466/
 * @help Adds a script call, e.g. for Conditional Branch:
 *      isKeyboardInputHeld()
 * Returns true or false.
 *
 * Free to use and/or modify for any project, no credit required.
 */
;void (() => {
"use strict";
  /** Private storage for {@linkcode isKeyboardInputHeld}. */
  const keysHeld = new Set();

  /** @returns {boolean} `true` iff a keyboard input is held. */
  const isKeyboardInputHeld = function() { return keysHeld.size > 0; };
  globalThis.isKeyboardInputHeld = isKeyboardInputHeld;  // to global

  /** @param {KeyboardEvent} event @returns {number} Corresponding `keyCode`. */
  const keyCode = function(event) { return event.keyCode; };

  // Patch - update `keysHeld` when appropriate.
  void (alias => {
    Input._onKeyDown = function(event) {
      alias.apply(this, arguments);
      const k = keyCode(event);
      if (k in this.keyMapper) {
        const name = this.keyMapper[k];
        if (name && this._currentState[name])
          keysHeld.add(k);
      }
    };
  })(Input._onKeyDown);

  // Patch - update `keysHeld` when appropriate.
  void (alias => {
    Input._onKeyUp = function(event) {
      alias.apply(this, arguments);
      const k = keyCode(event);
      if (k) {
        const name = this.keyMapper[k];
        if (name && !this._currentState[name])
          keysHeld.delete(k);
      }
    };
  })(Input._onKeyUp);

})();


@bass2yang - the core Input._latestButton implementation is not robust for that purpose: it is only designed for tracking the press time for the latest input received. If you hold, say, Shift, then press & release Enter, Shift is still held but _latestButton will be null (cf Input.update, rmmz_core).

Hey, it works! But only for the buttons that are mapped by default, like Z, X, shift, etc...
What I need is any button, as in everything on the keyboard.
Maybe I need them mapped first, but I have no idea how.
What I need is any button, as in everything on the keyboard.
Ah, I added checks specifically to restrict it to unfiltered keyMapper keys. To include unmapped keys, you can try removing (or // commenting out) these lines:
JavaScript:
        const name = this.keyMapper[k];
        if (name && this._currentState[name])
JavaScript:
        const name = this.keyMapper[k];
        if (name && !this._currentState[name])

Original thread: https://forums.rpgmakerweb.com/threads/check-if-player-is-pressing-any-button-on-keyboard.175466/