Search code examples
javascriptqualtrics

Quadratics: Replace Keyboard stroke for Mouse clicks


I am not advanced with Javascript. i was hoping for someone to simply explain the process to edit the following code.

this.hideNextButton();
this.hidePreviousButton();

var that = this;

Event.observe(document, 'keydown', function keydownCallback(e) {
  var choiceID = null;

  switch (e.keyCode) {
    case 74: // 'j' was pressed
      choiceID = 1;
      break;
    case 75: // 'k' was pressed
      choiceID = 2;
      break;
  }

  if (choiceID) {
    Event.stopObserving(document, 'keydown', keydownCallback);
    that.setChoiceValue(choiceID, true);
    that.clickNextButton();
  }
});

If a user wants to replace the click of a mouse with clicking a letter on the Keyboard, eg. Click J for the next question or to select yes, click A. I think that is what this code is doing but I'd like to pull it apart a bit to add or remove letters to complete additional tasks, such as next question etc.

Any help or pointing in the right direction is a help!


Solution

  • In the code you provided pressing 'j' or 'k' answers the current question by setting the choice value and goes to the next page. To add other keyboard presses you would additional cases to the switch using the appropriate keycode. For example, if you wanted 'j' to just go to the next page and 'a' to answer 'Yes', it would be something like this (remove the if(choiceID) section):

    Event.observe(document, 'keydown', function keydownCallback(e) {
      switch (e.keyCode) {
        case 65:  // 'a' was pressed
          that.setChoiceValue(1, true);
          break;
        case 74: // 'j' was pressed
          Event.stopObserving(document, 'keydown', keydownCallback);
          that.clickNextButton();
      }
    });