Defining specific keypress to progress | XM Community
Skip to main content

I am using the code below so participants can press a key to respond and then immediately progress to the next page. I want to limit the keys that will register to r, b, g, and y. What do I need to modify in the code I have already so that the page only progresses when one of those keys is pressed?
Qualtrics.SurveyEngine.addOnReady(function() {
   var input = jQuery("#"+this.questionId+" inputptype=text]");
   input.select().focus(); //works on desktop, but not on all mobile browsers
   input.keydown(function(e) { if(e.which == 13) jQuery("#NextButton").click(); });
});
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: // 'spacebar' was pressed
choiceID = 1;
break;
}
if (choiceID = 1) {
Event.stopObserving(document, 'keydown', keydownCallback);
that.setChoiceValue(choiceID, true);
that.clickNextButton();
}
});
});

You can just create a do while and if the specified key was pressed set a variable as 1 using setEmbeddedData functionality of Qualtrics's javaScript. I don't think you need multiple keydown functions.


nrf23,
You need to check for the keys your are looking for (r, b, g, y) inside the keydown event handler.
The current code looks like it was just copied and pasted together from various sources and much different from what it should actually be.
If by chance you are trying to do a Stroop Test, see this.


Leave a Reply