Recording reaction time with space bar (but for text entry NOT multiple choice) | XM Community
Skip to main content
Hi,



I'd like to record the time it takes participants to press the space bar without it moving on to the next page. I'm doing Remote Associate Tests and would like the option for the participant to press the spacebar when they have come up with the solution, then they can type it out. As Qualtrics only seems to time how long you're on the page for, I'd like to incorporate this key press RT so I know how long it takes for them to think of the answer (not just type it out).



What I've found on this forum and others is a code along the lines of...



Qualtrics.SurveyEngine.addOnload(function() {

var qid = this.questionId;



document.onkeydown = function(event) {

console.log('keydown',event);

if (event.which == 32) {

event.preventDefault();

jQuery('#NextButton').click();

}

}



});

However this doesn't seem to be working. Most of the codes I've seen are for multiple choice questions, but all of mine are text entries.



I'm a real novice with Javascript, so any advice would be very welcome!



Thanks
Hi @icer_j,

I'm not sure whether I got right what you want to do since I don't understand why do you take care about question type.

Nevertheless following code gives you the time from the beginning till the first pressing of the space regardless on which object is focused (and also when no object is focused, therefore there is the `window.top.document.addEventListenerpart `of the code).

In case you want to record the time of the space hit only if the user clicks into the text box you can use `jQuery("[id='QR~"+this.questionId+"']").keydown(function(){ ...`



Qualtrics.SurveyEngine.addOnReady(function() {

var start = new Date();



function getTime(e)

{ e = e || window.event;

var key = e.which || e.keyCode;

if(key===32){

var t = new Date() - start;

alert(t);

document.removeEventListener('keydown', getTime);

window.top.document.removeEventListener('keydown', getTime);

}

}



document.addEventListener("keydown", getTime);

window.top.document.addEventListener('keydown', getTime);

});
Hi,



Thanks very much for your response. The code doesn't seem to be working with my question? I've tried entering it with 'Add Javascript' on both the question itself and the timing component. Do you have any further advice on where to add the code to make it record the time it takes to press the spacebar?
> @icer_j said:

> Hi,

>

> Thanks very much for your response. The code doesn't seem to be working with my question? I've tried entering it with 'Add Javascript' on both the question itself and the timing component. Do you have any further advice on where to add the code to make it record the time it takes to press the spacebar?



Hi @icer_j,

the code records and stores it in a variable and shows it in a pop-up window. Does this work? However, the code doesn't store the result in your output data.

If you want to get this, you'll have to store your time in an embedded field. You can do it like this:

`Qualtrics.SurveyEngine.setEmbeddedData( 'embeddedDataVariableName', t );`

Don't forget to define your embedded field in the survey flow manually.
Hi @fleb,



Neither the pop-up window nor the embedding seems to work the way I've done it. Here's what I've got in the Javascript now for one of the questions:



Qualtrics.SurveyEngine.addOnload(function()

{

/*Place your JavaScript here to run when the page loads*/



});



Qualtrics.SurveyEngine.addOnReady(function()

{

Qualtrics.SurveyEngine.addOnReady(function() {

var start = new Date();



function getTime(e)

{ e = e || window.event;

var key = e.which || e.keyCode;

if(key===32){

var t = new Date() - start;

alert(t);

document.removeEventListener('keydown', getTime);

window.top.document.removeEventListener('keydown', getTime);

}

}



document.addEventListener("keydown", getTime);

window.top.document.addEventListener('keydown', getTime);

});



});



Qualtrics.SurveyEngine.addOnUnload(function()

{

Qualtrics.SurveyEngine.setEmbeddedData( 'embeddedsolutiontime', t )



});



In Survey flow I've set Embedded Data with the variable 'solutiontime'...



I'm sure I'm missing something crucial here, or else I've put the script in the wrong place perhaps?
Hi @icer_j,

1) you have there `Qualtrics.SurveyEngine.addOnReady(function() {` and `}) `twice

2) `Qualtrics.SurveyEngine.setEmbeddedData( 'embeddedsolutiontime', t ) should` be also inside Qualtrics `OnReady` function. The `OnUnlouad` function doesn't know `t`.
Hi @fleb,



I've made the amendments and yes, the time it takes for the user to hit spacebar appears in a popup window. However I'm still struggling to get it to record this time in the data.



My code reads:



Qualtrics.SurveyEngine.addOnReady(function() {

var start = new Date();



function getTime(e)

{ e = e || window.event;

var key = e.which || e.keyCode;

if(key===32){

var t = new Date() - start;

alert(t);

document.removeEventListener('keydown', getTime);

window.top.document.removeEventListener('keydown', getTime);

}

}



document.addEventListener("keydown", getTime);

window.top.document.addEventListener('keydown', getTime);

Qualtrics.SurveyEngine.setEmbeddedData( 'embeddedsolutiontime', t )

});





I've defined 'solutiontime' under survey flow, I haven't set a value (it reads 'value will be set from Panel or URL'). I've left the variable as text and selected 'Analyze Text'. The 'solutiontime' variable appears in the data spreadsheet however no time values are recorded.
Hi,

this line `Qualtrics.SurveyEngine.setEmbeddedData( 'embeddedsolutiontime', t )` has to be there, where `t` is defined. That is inside `if` inside the `getTime` function.

So the whole code should look like this:



Qualtrics.SurveyEngine.addOnReady(function() {

var start = new Date();



function getTime(e)

{ e = e || window.event;

var key = e.which || e.keyCode;

if(key===32){

var t = new Date() - start;



Qualtrics.SurveyEngine.setEmbeddedData( 'embeddedsolutiontime', t)

alert(t);

document.removeEventListener('keydown', getTime);

window.top.document.removeEventListener('keydown', getTime);





}



}



document.addEventListener("keydown", getTime);

window.top.document.addEventListener('keydown', getTime);





});
Hi, thanks very much, the code works! But I'm still having trouble getting the data in the Excel. solutiontime is still empty on Excel, I've tried downloading the data as CSV and CSV using Legacy Format and had no luck
> @icer_j said:

> Hi, thanks very much, the code works! But I'm still having trouble getting the data in the Excel. solutiontime is still empty on Excel, I've tried downloading the data as CSV and CSV using Legacy Format and had no luck



Hi @icer_j,

and do you see the solutiontime when you click on Data&analysis in Qualtrics? And is the column empty or missing?



Few tips:

1) Make sure, that your embedded field defined in the flow before the question with the code.

2) Don't forget that you must press the space, otherwise the field will be empty.

3) And in case you have a lot of columns in your survey, try to look carefuly once again if



I've checked this code and it worked...
Hi @fleb,



I've done all of the above and it is still empty in the data. In Data & Analysis the column was missing entirely
> @icer_j said:

> Hi @fleb,

>

> I've done all of the above and it is still empty in the data. In Data & Analysis the column was missing entirely



Hi @icer_j,

I have checked the code again and it worked. In case you have defined the column manually in the survey flow, it should be there in any case. If the script were wrong, the column should be empty but not missing. Isn't it just hidden? If you aren't sure, try to check it like this.



!



Otherwise I have no more ideas where could be the problem, sorry.
Hi @fleb



Thanks very much for all your help. I changed my variable name from 'solutiontime' to 'embeddedsolutiontime' then copied your code again. I needed a space between t and ) in



Qualtrics.SurveyEngine.setEmbeddedData( 'embeddedsolutiontime', t)



but now it is recording the time it takes to hit the spacebar in my excel spreadsheet.



Thanks once again!

Leave a Reply