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,087
Reaction score
1,508
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

Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD
How many parameters is 'too many'??

Forum statistics

Threads
105,860
Messages
1,017,040
Members
137,569
Latest member
Shtelsky
Top