Registering key-presses | XM Community
Solved

Registering key-presses


I used to use javascript in qualtrics to let respondents to respond by an 'h' or 'l' key-press on the keyboard. Apparently qualtrics made some changes in the program as it does not register the specific key that respondents pressed in the data file. I've contacted qualtrics support about this. They directed me to this forum for help. Can somebody advice me how to make qualtrics to register key-presses and continue to the fallowing question?
icon

Best answer by LaurenK 10 July 2018, 18:11

View original

13 replies

Userlevel 7
Badge +27
Post your current script.
I've found three scripts on the internet. None of them worked.

Script 1

Qualtrics.SurveyEngine.addOnload(function()
{
if ($('NextButton'))
$('NextButton').hide();
if ($('PreviousButton'))
$('PreviousButton').hide();

var that = this;
Event.observe(document,'keydown',function(e){
var choiceID = null;
if (e.keyCode == 76) //'l' was pressed
{
choiceID = 1;
}
else if (e.keyCode == 72) //'h' was pressed
{
choiceID = 2;
}
if (choiceID)
{
that.setChoiceValue(choiceID,true);
that.clickNextButton();
}
});

});


Script 2

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 72: // 'h' was pressed
choiceID = 1;
break;
case 76: // 'l' was pressed
choiceID = 2;
break;
}

if (choiceID) {
Qualtrics.SurveyEngine.setEmbeddedData("choiceID",choiceID);
Event.stopObserving(document, 'keydown', keydownCallback);
that.setChoiceValue(choiceID, true);
that.clickNextButton();
}

});
});

Script 3

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();
}
});
});
Userlevel 7
Badge +27
Try changing addOnload to addOnReady.
changing addOnload to addOnReady had no effect on registration of key-presses. Do you have another suggestion?
Userlevel 7
Badge +27
Are you in preview mode? If so, you have to click in the desktop window first.
No i'm not in review mode, I've distributed and completed the survey by anonymous link.
Userlevel 7
Badge +13
Hey @KD18! Check out this community post, as well as this post!
@LaurenK,
Thank you for your latest response. Unfortunately, also the scripts mentioned in the links in your response does not register responses (the second link does not even let me proceed to the next page). I'm still struggling with this puzzle, any help is appreciated.
@TomG I've tried the three scripts I mentioned in my post on June 2, unfortunately none of them worked.
Userlevel 6
Badge +6
Lauren is correct, the JavaScript I wrote for Left and Right arrow keys will work here: https://gist.github.com/mattbloomfield/e35b360fb259df583e17020abcec4ca9

You need to change the key mappings though. Your browser listens for key presses and there is a numeric code associated with each key, as shown here: http://keycode.info/

Working Example: https://qualtricssfi.az1.qualtrics.com/jfe/preview/SV_6KzTr6Nr0UfPJSl?Q_SurveyVersionID=current&Q_CHL=preview

.qsf attached -- and as Lauren pointed out, in Preview you do have to click the survey for it to register clicks. This is because it is iFramed in rather than presented as the main scope.
Userlevel 1
@mattyb513 just wanted to say that your code works for me. Thank you. This example made it simple to learn how to get keyboard responses.
@mattyb513, @PsychSurvey, @LaurenK, @TomG, thank you very much for your help!! The script provided by mattyb513 works for me too 🙂 Again thanks a lot for your help. Much appreciated!

Hi all,
I'm clearly a bit late to the party.
mattyb513 i'm using the code you offered in the above thread, changing the arrow up/down keys for the keys V, B and N.
I would like participants to respond v, b, or n, which are associated with a different answer. I've edited the code (below) to add an additional key. Is this correct?
I'm also wondering if there is a way to apply this code to multiple blocks at once (rather than going into each and inserting the code)?
TIA,
Hannah

Qualtrics.SurveyEngine.addOnload(function() {
   var qid = this.questionId;

   document.onkeyV = function(event) {
      console.log('keyV',event);
      if (event.which == 86) {
         event.preventDefault();
         Qualtrics.SurveyEngine.registry[qid].setChoiceValue(1, true);
         jQuery('#NextButton').click();
      } else if (event.which == 78) {
         event.preventDefault();
         Qualtrics.SurveyEngine.registry[qid].setChoiceValue(2, true);
         jQuery('#NextButton').click();
      } else if (event.which == 66) {
         event.preventDefault();
         Qualtrics.SurveyEngine.registry[qid].setChoiceValue(3, true);
         jQuery('#NextButton').click();
      }
   }

});

Leave a Reply