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.