MV How to add _handler key to a scene?

Hello!

I need to add a handler to a window. I was thinking of using a simple target.addEventListener, but I realized I want to use this for the window.

I tried adding it as follows:
JavaScript:
    this._buyWindow.setHandler('control', this.onCompare.bind(this)); //Adding onCompare handler
    this._buyWindow.width = ww;
    this.addWindow(this._buyWindow);
};

Scene_Shop.prototype.onCompare = function() {
    console.log(this);
};

As found in rpgcore.js:
JavaScript:
Input.keyMapper = {
    9: 'tab',       // tab
    13: 'ok',       // enter
    16: 'shift',    // shift
    17: 'control',  // control
    18: 'control',  // alt

When it did not work, I looked into what the possible keys in Input.keyMapper was, I then noticed that there was no 'cancel'. And since some, or most, windows and such utilize the symbol 'cancel'. It became clear that this was not the place to look...

I then went into Window_Selectable. But sadly it is so much new information that I decided to just post and request some assistance! 🙂 As always, I will keep going over the information available to me and update as I go.

So, how do I enable it so that when a player presses ctrl/alt the function onCompare triggers?

Best regards,

Update:
So I managed to get it working, sort of...
JavaScript:
Window_ShopBuy.prototype.processCursorMove = function() {
    if (this.isCursorMovable()) {
        var lastIndex = this.index();
        if (Input.isRepeated('down')) {
            this.cursorDown(Input.isTriggered('down'));
        }
        if (Input.isRepeated('up')) {
            this.cursorUp(Input.isTriggered('up'));
        }
        if (Input.isRepeated('right')) {
            this.cursorRight(Input.isTriggered('right'));
        }
        if (Input.isRepeated('left')) {
            this.cursorLeft(Input.isTriggered('left'));
        }
        if (!this.isHandled('pagedown') && Input.isTriggered('pagedown')) {
            this.cursorPagedown();
        }
        if (!this.isHandled('pageup') && Input.isTriggered('pageup')) {
            this.cursorPageup();
        }
        if (!this.isHandled('control') && Input.isTriggered('control')) {
            this.onCompare();
            SoundManager. playCursor();
        }
        if (this.index() !== lastIndex) {
            SoundManager. playCursor();
        }
    }
};
This way, I get the this part and it happens upon using ctrl/alt within the shopBuy window.
However, this is not dependent on an event handler for the Scene. So now this refers to the window rather than the scene. This could complicate certain aspects, but I am sure it should work.

I am still interested in understand how to add an event handler 🙂
Yes: as you discovered, the handler symbol is not treated as a button name. setHandler simply stores the given callback under the given symbol. The isHandled and callHandler methods are then used to check whether a given symbol is handled, and if so then invoke the appropriate response.

For this, you would typically extend the window's processHandling method:
Spoiler: Window_Selectable#processHandling (rpg_windows.js)
JavaScript:
Window_Selectable.prototype.processHandling = function() {
    if (this.isOpenAndActive()) {
        if (this.isOkEnabled() && this.isOkTriggered()) {
            this.processOk();
        } else if (this.isCancelEnabled() && this.isCancelTriggered()) {
            this.processCancel();
        } else if (this.isHandled('pagedown') && Input.isTriggered('pagedown')) {
            this.processPagedown();
        } else if (this.isHandled('pageup') && Input.isTriggered('pageup')) {
            this.processPageup();
        }
    }
};
E.g.
JavaScript:
(function(alias) {
  Window_ShopBuy.prototype.processHandling = function() {
    alias.apply(this, arguments);
    if (this.isOpenAndActive()) {
      if (this.isHandled('control') && Input.isTriggered('control')) {
        this.callHandler('control');
      }
    }
  };
})(Window_ShopBuy.prototype.processHandling);
processHandling is invoked as part of the update method of Window_Selectable. 👋

[Some minor edits for clarity.]

Original thread: https://forums.rpgmakerweb.com/threads/how-to-add-_handler-key-to-a-scene.141577/