Handle keyboard events on specific scene

Keyguard

Warper
Member
Joined
Jul 17, 2020
Messages
1
Reaction score
1
First Language
Russian
Primarily Uses
RMMV
Hello all,

New to RPGMMV and to plugin development, however have some basic experience with JS. Is there a way of implementing keyboard events on specific scene (e.g. one that I will create in plugin). I found SceneBase.addListener in the API but it seems that it only allows to add mouse/joystick event handlers. In my case, I want to rebind keyboard action, but do it only in a context of a specific scene. Something like this (in pseudocode):

JavaScript:
Scene.somekeyhandlerthatIcouldnotfound = function(keyCode) {
      switch (keyCode) {
      case 33:    // pageup
          // do something
      case 40:    // down arrow
          // do something else
      }
      return false;
  };
Sorry for may be stupid question and thanks in advance!

Updated: just to clarify, I'm aware of remapping Input._onKeyDown in rpg.core.js, just wanted to find more elegant way to do it :).
 
Last edited:

caethyril

^_^
Veteran
Joined
Feb 21, 2018
Messages
2,056
Reaction score
1,482
First Language
EN
Primarily Uses
RMMZ
The SceneManager runs the update method of the active scene every frame. Typically, scene-specific inputs/responses are part of that update routine. E.g. this is where the map scene checks the input each frame to see if the player has opened the menu:
JavaScript:
Scene_Map.prototype.update = function() {
    this.updateDestination();
    this.updateMainMultiply();
    if (this.isSceneChangeOk()) {
        this.updateScene();   // <- A
    } else if (SceneManager.isNextScene(Scene_Battle)) {
        this.updateEncounterEffect();
    }
    this.updateWaitCount();
    Scene_Base.prototype.update.call(this);
};

Scene_Map.prototype.updateScene = function() {
    this.checkGameover();
    if (!SceneManager.isSceneChanging()) {
        this.updateTransferPlayer();
    }
    if (!SceneManager.isSceneChanging()) {
        this.updateEncounter();
    }
    if (!SceneManager.isSceneChanging()) {
        this.updateCallMenu();   // <- B
    }
    if (!SceneManager.isSceneChanging()) {
        this.updateCallDebug();
    }
};

Scene_Map.prototype.updateCallMenu = function() {
    if (this.isMenuEnabled()) {
        if (this.isMenuCalled()) {   // <- C
            this.menuCalling = true;
        }
        if (this.menuCalling && !$gamePlayer.isMoving()) {
            this.callMenu();
        }
    } else {
        this.menuCalling = false;
    }
};

Scene_Map.prototype.isMenuCalled = function() {
    return Input.isTriggered('menu') || TouchInput.isCancelled();
};
So you could write your own little "check for custom inputs" method and call that in your scene's update method, e.g.
JavaScript:
Scene_Custom.prototype.update = function() {
  // stuff?
  this.updateInput();
  // other stuff?
};

Scene_Custom.prototype.updateInput = function() {
  // cf Input definitions in rpg_core.js, including keyMapper property
  if (Input.isPressed('pageup')) {
    // do something
  } else if (Input.isPressed('down')) {
    // do something else
  }
};
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Do you have an audio you want me to animate? If so send me the youtube link and I’ll animate it.
Okay, so I'm working on Draft 2 of my game's script.
Will try to keep you guys posted, if you're interested!

This is vid of my new game, Crystalgazers. A demo is available on my Gamejolt

I just thought of the best achievement for my game: "'I Didn't Know You Were a Developer!' Find and enter one or more dev room(s)."
I made this animation to apply to a job, what do you think?

Forum statistics

Threads
105,587
Messages
1,014,816
Members
137,253
Latest member
Koshibari
Top