Using javascript to record how many times a button is pressed | XM Community
Skip to main content

Hi,
I'm asking participants to press the space bar 5 times when presented a specific question, and so would like to record how many times they press the space bar when presented with the question. Is this possible using javascript?
Thanks in advance!

Use the below code:
Qualtrics.SurveyEngine.addOnReady(function() {                                          
 var i = 0;                                                 
 document.onkeydown = function(event) {                                                
if(event.which == 32){                                                             
     event.preventDefault();                                                           
     i++;                                                                            
     Qualtrics.SurveyEngine.setEmbeddedData("count",i);                    
   }
 }
});


Thank you very much for your reply!
I've added that code to the JS of the question (see attached printscreen), and I've previewed the survey several times, but when I export the dataset, it doesn't seem to have sized a variable with the number of spacebar clicks.
Have I missed something?
Screenshot 2020-07-07 at 16.12.26.png


Have you created embedded data - "count" at the start of survey flow?


Hello,
Did you ever get this working?
I am trying to do something similar but with the "D" key. See below:
image.pngI have created an embedded variable "count" at the start of the survey flow, but nothing seems to be registering when pressing "D". Could you tell me exactly how the embedded variable should look in the survey or let me know if you see any errors?
It might be important to note that I am trying to register the number of key presses while the participant watches a video in full screen, hence the full screen code.
Thank you for your help!


Leave a Reply