Autoadvance and Record Key Pressed | XM Community
Solved

Autoadvance and Record Key Pressed

  • 26 January 2021
  • 2 replies
  • 57 views

Badge

Hello, Javascript Newbie here!
I am programming an experiment where people see stimuli and have to decide if they are the same or different - and for this they press 1 and 2, respectively. Participants see the stimuli for a set of time and then are presented a multiple choice with the two options; I specifically want them to use the keys, instead of just using the mouse. I want the next trial to start whenever one of the two keys is pressed. In the end, I want the database to have their answers recorded - that is what I am struggling with! The field for the answers always comes out empty.
Through foruns here and there, I put together the following code. Can you help me out? What should I change so that when I download the data I can see whether participants pressed 1 or 2?

Qualtrics.SurveyEngine.addOnload(function()
{
this.hideNextButton(); //Blocks from advancing
this.hidePreviousButton(); //Blocks from retreating
var that = this;
Event.observe(document, 'keydown', function keydownCallback(e) { //Set values for keys
  var choiceID = null;
switch (e.keyCode) {
  case 49: // '1' was pressed
choiceID = 1;
 }
switch (e.keyCode) {
  case 50: // '2' was pressed
choiceID = 2;
 }
 if (choiceID) {
  Event.stopObserving(document, 'keydown', keydownCallback);
that.setChoiceValue(choiceID, true);
  that.clickNextButton();
 } //Advance when key are pressed
});
});

icon

Best answer by fcruz27 27 January 2021, 10:28

View original

2 replies

Userlevel 7
Badge +21

Take a look here and see if this solves your problem... https://www.qualtrics.com/community/discussion/13976/keyboard-responses-not-recorded-as-answers#latest

Badge

Thank you, Ahmed!
I was about to come here and share the answer with whoever might be facing the same issue - I found the answer later yesterday
When I created a new question, it worked fine - the issue was with the choiceIDs: if you have, for example, two options the choiceIDs will be 1 and 2, respectively; imagine that you add another option (choiceID = 3) and then delete one of the former (choiceID = 1) - you will be left with 2 and 3. If your code registers 1 and 2,if anyone answer 1 the answer won't be recorded!
So, everyone make sure the choiceID that you set your value into is the same assigned to the options you have there.

Leave a Reply