Editable TextBox component does not register keypresses after updating to version 2021.1.2

glad to be an inspiration some how :slight_smile:
and thanks for the update!
in another thread @sotiri suggested this code component in the Begin Routine tab.

core.Keyboard.prototype._addKeyListeners = function() {
  this._previousKeydownKey = undefined;
  const self = this;
  window.addEventListener("keydown", (event) => {
    if (event.repeat) {
      return;
    }
    const timestamp = util.MonotonicClock.getReferenceTime();
    if (this._status !== PsychoJS.Status.STARTED) {
      return;
    }
    self._previousKeydownKey = event.key;
    let code = event.code;
    if (typeof code === 'undefined') {
      code = core.EventManager.keycode2w3c(event.keyCode);
    }
    let pigletKey = core.EventManager.w3c2pyglet(code);
    self._bufferIndex = (self._bufferIndex + 1) % self._bufferSize;
    self._bufferLength = Math.min(self._bufferLength + 1, self._bufferSize);
    self._circularBuffer[self._bufferIndex] = {
      code,
      key: event.key,
      pigletKey,
      status: core.Keyboard.KeyStatus.KEY_DOWN,
      timestamp
    };
    self._unmatchedKeydownMap.set(event.code, self._bufferIndex);
    self._psychoJS.logger.trace('keydown: ', event.key);
    event.stopPropagation();
  });
}

based on what you said- do you think it will help? i cannot test it until Pavlovia will be back

If @sotiri says so, probably yes; he has quite some skillz

1 Like

he was right! it works :slight_smile:
thank you @thomas_pronk and @sotiri i am marking it as solution for others until the bug is fixed.

1 Like

Hi! I am experiencing the same issue and the code above does nothing to improve it. @chayabenyehuda Did it help you out in the end?

Best

Same here-- no luck pasting the above code in the begin routine tab (though it doesn’t crash the experiment either). Should any of this be changed for our individual experiment? Anything different on mac vs pc?

Text box is still not editable online; though strangely I can highlight the default text. Also, I get this error very briefly after quitting via the escape key, before the regular quit message:
image

TypeError: this._last_renderer.view is null
    _getDOMRelativeWorldTransform

However, this only happens if I have tried to type something.

Hi @ajus, @aisa2, which PsychoJS version are your projects based on currently? I believe 2021.1.3 no longer has this the problem, x

Hi @sotiri, I was in 2021.1.2 when I made that post. I just upgraded to 2021.1.3; now the error message doesn’t appear, but I am still unable to type text. I have removed and re-added the textbox component to make sure nothing is carrying over, but to no avail.

OK could you send me a link to the project? Thanks, x

Done, thanks for looking into this!

Hi! I am on 2021.1.3 as well. The textbox is editable, but I have to press every key for a while until the letter appears. Thanks for helping!

Hi @ajus, thanks for flagging, the fix is in master now and should be included with the 2021.1.4 release in prep as we speak, x

Hi all,

Based on troubleshooting with @sotiri, the temporary solution is to put this code snippet in the “Before Experiment” section of one of your code components on the JS side (NOT begin routine as stated above). But none of this will be needed once 2021.1.4 is released.

core.Keyboard.prototype._addKeyListeners = function()
{
  this._previousKeydownKey = undefined;
  const self = this;
  window.addEventListener("keydown", (event) =>
    {
      if (event.repeat)
      {
        return;
      }
      const timestamp = util.MonotonicClock.getReferenceTime();
      if (this._status !== PsychoJS.Status.STARTED)
      {
        return;
      }
      self._previousKeydownKey = event.key;
      let code = event.code;
      if (typeof code === 'undefined')
      {
        code = core.EventManager.keycode2w3c(event.keyCode);
      }
      let pigletKey = core.EventManager.w3c2pyglet(code);
      self._bufferIndex = (self._bufferIndex + 1) % self._bufferSize;
      self._bufferLength = Math.min(self._bufferLength + 1, self._bufferSize);
      self._circularBuffer[self._bufferIndex] = {
        code,
        key: event.key,
        pigletKey,
        status: core.Keyboard.KeyStatus.KEY_DOWN,
        timestamp
      };
      self._unmatchedKeydownMap.set(event.code, self._bufferIndex);
      self._psychoJS.logger.trace('keydown: ', event.key);
      event.stopPropagation();
      // event.preventDefault();
    }
  );
  window.addEventListener("keyup", (event) =>
    {
      const timestamp = util.MonotonicClock.getReferenceTime();
      if (this._status !== PsychoJS.Status.STARTED)
      {
        return;
      }
      self._previousKeydownKey = undefined;
      let code = event.code;
      if (typeof code === 'undefined')
      {
        code = core.EventManager.keycode2w3c(event.keyCode);
      }
      let pigletKey = core.EventManager.w3c2pyglet(code);
      self._bufferIndex = (self._bufferIndex + 1) % self._bufferSize;
      self._bufferLength = Math.min(self._bufferLength + 1, self._bufferSize);
      self._circularBuffer[self._bufferIndex] = {
        code,
        key: event.key,
        pigletKey,
        status: core.Keyboard.KeyStatus.KEY_UP,
        timestamp
      };
      const correspondingKeydownIndex = self._unmatchedKeydownMap.get(event.code);
      if (typeof correspondingKeydownIndex !== 'undefined')
      {
        self._circularBuffer[self._bufferIndex].keydownIndex = correspondingKeydownIndex;
        self._unmatchedKeydownMap.delete(event.code);
      }
      self._psychoJS.logger.trace('keyup: ', event.key);
      event.stopPropagation();
    }
  );
};
1 Like

Great, thanks a lot!