Collect the number or key presses on a computer keyboard while watching a video clip | XM Community
Skip to main content

Hello!
I'm designing a survey in which I wish to collect the exact number of times a participant presses the Space bar (or possibly another key on the keyboard) while watching a video clip. That is, participants are asked to press the key as many times as they wish, and the number of presses is my main outcome variable. It is therefore important that the number of recorded presses corresponds to the actual number they pressed the key. Some participants will do this experiment on their home computer so this needs to work on multiple browsers.
For a previous in-lab experiment we simply added a text-entry field, hidden to participants, as a separate question under the video. Using some code, we collected the number of Space bar presses made by participants. In order to make sure that if a participant lingered on a key, we changed the preferences on the computer itself to make sure that one press could only result in one character, no matter the time a person pressed a key. This worked for the time being (I counted the number of space characters by hand afterwards in the dataset...) but I need a more secure and neater way this time around. On that will work online too, no matter the computer and browser used.
I am wondering if anyone knows of a better solution for recording the number of key presses for my purposes?
Best wishes and thanks in advance!
Sofia

You can use this code to collect the number of times space is pressed. It will be available in an embedded data as

space_counter
. Just define it in the survey flow before the question, otherwise, you won't see it in your downloaded data.
Qualtrics.SurveyEngine.addOnload(function () {
    let counter = 0;
    function key_counter(e) {
        if (e.keyCode == 32) {
            counter++;
            Qualtrics.SurveyEngine.setEmbeddedData("space_counter", counter);
        }
    }
    Event.observe(document, "keyup", key_counter);
    document.querySelector("#NextButton").onclick = function () {
        Event.stopObserving(document, "keyup", key_counter);
    };
});



Thank you so so much, this works perfectly!


Hi ahmedA ,

I am using a similar method, except wishing to record the number of "x"s pressed instead of space bar. Do you know what adjustments i would need to make to this code?
Also, how would i define this before the question to ensure my data is recorded?

Thanks in advance,
Harri


https://community.qualtrics.com/XMcommunity/discussion/comment/34430#Comment_34430Would you be able to comment on how you did this? I am very new and am unsure of how to embed this and define it in the survey flow.


For other digits, look up the key codes online and change appropriately. Add one if block for each key.


Leave a Reply