Adding keyboard responses to a Qualtrics survey | XM Community
Skip to main content

Hey. I am currently designing a survey, and I have added a question at the beginning of the survey asking respondents to make one of two button responses to either continue or exit the survey. For example, respondents are required to press the "J" button if they wish to continue or the "esc" button to exit the survey.
I have found a code (see below) that answers the first aim, where pressing "J" button progresses to the next question. However, is there a line of code that I can add to this existing code for respondents who wish to terminate the survey? So a line of code that will replace "..  case 75: // 'k' was pressed.." with one that exits the survey. I am a newbie to Javascript, so help will be greatly appreciated 😃.

Qualtrics.SurveyEngine.addOnload(function()
{
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();
 }
 });
});

Be the first to reply!

Leave a Reply