I want a certain window of mine to listen to keyboard events, when it is shown and don't listen to them, when it is hidden. The problem is document.removeEventListener seems not to work, not to throw any exception and always return undefined, so I cannot figure out why it does not work. From the description of the function I also cannot figure out why. I don't use any of the optional parameter, so I thought it should work.
Here is some code to test out what I mean. Just call SceneManager.push(Scene_EventListener) in the console. Even when the scene is popped and the window should not exist anymore, the onKeyPress Event is still functioning:
Here is some code to test out what I mean. Just call SceneManager.push(Scene_EventListener) in the console. Even when the scene is popped and the window should not exist anymore, the onKeyPress Event is still functioning:
Code:
function Scene_EventListener()
{
this.initialize.apply(this, arguments);
}
Scene_EventListener.prototype = Object.create(Scene_MenuBase.prototype);
Scene_EventListener.prototype.constructor = Scene_EventListener;
Scene_EventListener.prototype.initialize = function()
{
Scene_MenuBase.prototype.initialize.call(this, arguments);
}
Scene_EventListener.prototype.create = function()
{
Scene_MenuBase.prototype.create.call(this);
this._commandWindow = new Window_EventListenerCommand();
this._eventListenerWindow = new Window_EventListener();
this._commandWindow.setHandler('Show', this.onShow.bind(this));
this._commandWindow.setHandler('Hide', this.onHide.bind(this));
this._commandWindow.setHandler('cancel', this.popScene.bind(this));
this.addWindow(this._commandWindow);
this.addWindow(this._eventListenerWindow);
}
Scene_EventListener.prototype.onShow = function()
{
this._commandWindow._showEnabled = false;
this._commandWindow.refresh();
this._commandWindow.activate();
this._eventListenerWindow.show();
}
Scene_EventListener.prototype.onHide = function()
{
this._commandWindow._showEnabled = true;
this._commandWindow.refresh();
this._commandWindow.activate();
this._eventListenerWindow.hide();
}
Scene_EventListener.prototype.popScene = function()
{
this._eventListenerWindow.hide();
Scene_Base.prototype.popScene.call(this);
}
function Window_EventListenerCommand()
{
this.initialize.apply(this, arguments);
}
Window_EventListenerCommand.prototype = Object.create(Window_Command.prototype);
Window_EventListenerCommand.prototype.constructor = Window_EventListenerCommand;
Window_EventListenerCommand.prototype.initialize = function()
{
this._showEnabled = false;
Window_Command.prototype.initialize.call(this, 0, 0);
}
Window_EventListenerCommand.prototype.maxCols = function()
{
return 3;
}
Window_EventListenerCommand.prototype.windowWidth = function()
{
return SceneManager._screenWidth;
}
Window_EventListenerCommand.prototype.windowHeight = function()
{
return this.lineHeight() * 2;
}
Window_EventListenerCommand.prototype.makeCommandList = function()
{
this.addCommand('Show', 'Show', this._showEnabled);
this.addCommand('Hide', 'Hide', !this._showEnabled);
this.addCommand(TextManager.cancel, 'cancel');
}
function Window_EventListener()
{
this.initialize.apply(this, arguments);
}
Window_EventListener.prototype = Object.create(Window_Base.prototype);
Window_EventListener.prototype.constructor = Window_EventListener;
Window_EventListener.prototype.initialize = function()
{
this._text = "";
Window_Base.prototype.initialize.call(this, 0, this.lineHeight() * 2, SceneManager._screenWidth, SceneManager._screenHeight - this.lineHeight() * 2);
document.addEventListener("keypress", this.onKeyPress.bind(this));
}
Window_EventListener.prototype.show = function()
{
document.addEventListener("keypress", this.onKeyPress.bind(this));
Window_Base.prototype.show.call(this);
}
Window_EventListener.prototype.hide = function()
{
document.removeEventListener("keypress", this.onKeyPress.bind(this));
Window_Base.prototype.hide.call(this);
}
Window_EventListener.prototype.onKeyPress = function(event)
{
let char = String.fromCharCode(event.charCode)
if( char )
{
this._text += char;
alert(this._text);
}
else
{
SoundManager.playBuzzer();
}
}