How to record which key is being pressed | XM Community
Skip to main content
Hi. I have added a key response ('j' and 'k'), and auto-advance features to my survey, despite they are working, the output doesn't seem to record which key is being pressed. Is there something that I missed adding into the javascript which makes it record which key is pressed?



This is the script that I used in my survey.



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: // 'j' was pressed

choiceID = 1;

break;

case 75: // 'k' was pressed

choiceID = 2;

break;

}

if (choiceID) {

Event.stopObserving(document, 'keydown', keydownCallback);

that.setChoiceValue(choiceID, true);

that.clickNextButton();

}

});



});



Qualtrics.SurveyEngine.addOnReady(function()

{

/*Place your JavaScript here to run when the page is fully displayed*/



});



Qualtrics.SurveyEngine.addOnUnload(function()

{

/*Place your JavaScript here to run when the page is unloaded*/



});
The code you posted requires that you have a one multiple choice question on the page with two choices where the choice ids are 1 and 2. If that's the case, then if choice 1 is selected, they pressed j. If choice 2 is selected they pressed k.
> @TomG said:

> The code you posted requires that you have a one multiple choice question on the page with two choices where the choice ids are 1 and 2. If that's the case, then if choice 1 is selected, they pressed j. If choice 2 is selected they pressed k.



Can I still use this code if my survey does not have multiple choice question? Because in the survey, participants need to watch a video and press j or k as yes or no to respond. Does that mean I need to change the code in order to record which key is being pressed in the output file?
> @phydan1324 said:

> > @TomG said:

> > The code you posted requires that you have a one multiple choice question on the page with two choices where the choice ids are 1 and 2. If that's the case, then if choice 1 is selected, they pressed j. If choice 2 is selected they pressed k.

>

> Can I still use this code if my survey does not have multiple choice question? Because in the survey, participants need to watch a video and press j or k as yes or no to respond. Does that mean I need to change the code in order to record which key is being pressed in the output file?



@phydan1324,

I don't understand what you are doing. If yes and no aren't answers to a question, what are they?
So participants need to press a key (j or k) after watching a video, which the key represents yes or no. But I dont have the two choices present on the page, they just press a key and advance. !

This is how it looks like on preview.
> @TomG said:

> > @phydan1324 said:

> > > @TomG said:

> > > The code you posted requires that you have a one multiple choice question on the page with two choices where the choice ids are 1 and 2. If that's the case, then if choice 1 is selected, they pressed j. If choice 2 is selected they pressed k.

> >

> > Can I still use this code if my survey does not have multiple choice question? Because in the survey, participants need to watch a video and press j or k as yes or no to respond. Does that mean I need to change the code in order to record which key is being pressed in the output file?

>

> @phydan1324,

> I don't understand what you are doing. If yes and no aren't answers to a question, what are they?



Thanks for the help! I think I have figured out how I can do it!

Would you be able to share how you figured out how to do this? I am doing something similar. phydan1324


Leave a Reply