Key Press Code | XM Community
Skip to main content

Hello,
I am using the following code to record key presses in my survey. For some reason, V and W are working fine, but when I add "D" I am getting an error. Does anyone see what is wrong with my code? I do not typically write code. Thank you!

Qualtrics.SurveyEngine.addOnReady(function() {                      
 var i = 0; 
var j = 0;
var k = 0;
 document.onkeydown = function(event) {                         
  if(event.key.toUpperCase() == "V"){                               
   event.preventDefault();                              
   i++;                                       
   Qualtrics.SurveyEngine.setEmbeddedData("V",i);           
  }
                        
 else if(event.key.toUpperCase() == ("W"){                               
   event.preventDefault();                              
   j++;                                       
   Qualtrics.SurveyEngine.setEmbeddedData("W",j);           
  }
 else if(event.key.toUpperCase() == ("D"){                               
   event.preventDefault();                              
   k++;                                       
   Qualtrics.SurveyEngine.setEmbeddedData("D",k);    
}   
});

Correction, this is the code:
 var i = 0; 
var j = 0;
var k = 0;
 document.onkeydown = function(event) {                         
  if(event.key.toUpperCase() == "V"){                               
   event.preventDefault();                              
   i++;                                       
   Qualtrics.SurveyEngine.setEmbeddedData("V",i);           
  }                   
 else if(event.key.toUpperCase() == "W"){                               
   event.preventDefault();                              
   j++;                                       
   Qualtrics.SurveyEngine.setEmbeddedData("W",j);           
  }
 else if(event.key.toUpperCase() == "D"){                               
   event.preventDefault();                              
   k++;                                       
   Qualtrics.SurveyEngine.setEmbeddedData("D",k);    
}  
});


Hi there, I am able to get this to work on my end, you might be missing a bracket? First, I set an Embedded Data element at the top of the Survey Flow and created fields for V, W, D, and set them all to a value of 0.
Then, I added your code to the addOnReady of the JavaScript for a Text/Graphic question:
Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/
var i = 0; 
var j = 0;
var k = 0;

 document.onkeydown = function(event) {   
 
  if(event.key.toUpperCase() == "V"){                               
   event.preventDefault();                              
   i++;                                       
   Qualtrics.SurveyEngine.setEmbeddedData("V",i);           
  }                   

 else if(event.key.toUpperCase() == "W"){                               
   event.preventDefault();                              
   j++;                                       
   Qualtrics.SurveyEngine.setEmbeddedData("W",j);           
  }

 else if(event.key.toUpperCase() == "D"){                               
   event.preventDefault();                              
   k++;                                       
   Qualtrics.SurveyEngine.setEmbeddedData("D",k);    
  }  

}

});


Leave a Reply