How to disable a keypress event? | XM Community
Solved

How to disable a keypress event?

  • 14 September 2020
  • 2 replies
  • 876 views

Hi Qualtrics experts,

I am programming a survey which relies on a question which uses a keypress event, and have encountered a problem where custom code that enables a keypress response appears to 'carry over' to subsequent questions.

The goal of my survey is to create a task where respondents choose between one of two pictures that appear on the right and left sides of the screen. To accomplish this, I have created a binary multiple choice question with one response option referring to the left side of the screen, and a second response option referring to the right side of the screen. Then, using custom javascript code, I have mapped in left/right key presses to replace the manual response for that question, and hidden the question description and response options so subjects have to use the key presses to make their responses. I've also hidden the Next Button. This question works perfectly.

The problem is in the subsequent questions: respondents should not be able to advance through the subsequent questions in the block, as they are programmed using the timer function so that they can only auto-submit after X seconds have passed. I've done this by setting the "enable submit after" parameter equal to the "auto advance after" parameter. However, I've found that those subsequent questions still respond to the keypress events, despite the timer settings. After some further testing, I've found that when I remove the custom javascript pertaining to the keypress event from the first question that the timer works again: respondents cannot advance until the auto advance time threshold has been reached.

So, my question is: Is there any way I can disable the keypress event after that first question to prevent this auto advancing? Below is the custom javascript I have included for the first question. Please let me know if anything about my question is unclear, or if more information is needed, and thank you for your time and help.

// Hide the question description and response options so subjects have to use the key presses 
Qualtrics.SurveyEngine.addOnload(function() {
jQuery("#"+this.questionId).hide();
});

// Map in the left and right key presses to respond
Qualtrics.SurveyEngine.addOnload(function() {
var qid = this.questionId;
document.onkeydown = function(event) {
console.log('keydown',event);
if (event.which == 37) {
event.preventDefault();
Qualtrics.SurveyEngine.registry[qid].setChoiceValue(1, true);
jQuery('#NextButton').click();
} else if (event.which == 39) {
event.preventDefault();
Qualtrics.SurveyEngine.registry[qid].setChoiceValue(2, true);
jQuery('#NextButton').click();
}
}
});

// Hide the next button
Qualtrics.SurveyEngine.addOnReady(function(){
this.hideNextButton();
});

Best wishes,
Thomas

icon

Best answer by rondev 14 September 2020, 08:52

View original

2 replies

Userlevel 7
Badge +22

In the onunload function try the below code:
document.onkeydown = function(event) {}

rondev: That worked perfectly! Thanks so much for your expeditious help.

And for complete clarity for future users who might be interested in this: I solved the problem by adding this javascript code to the question that immediately followed the keypress question:

// Remove the keypress event so subjects can't click through at will
Qualtrics.SurveyEngine.addOnUnload(function()
{
document.onkeydown = function(event) {}
});

Best,
Thomas

Leave a Reply