presenting different image based on key press

145 views
Skip to first unread message

emars...@gmail.com

unread,
Jan 12, 2016, 5:57:17 PM1/12/16
to jsPsych
Hi Josh!

Trying to create the following experiment: subject sees a list of four categories, selects one of the categories, and sees an image from that category. (There's more complication after that with limiting the number they can see from each category etc, but I'm still stuck on this first step).

I'm trying to use a single-stim trial to store a key press (either a, b, c, or d) and then to store that key press in a variable. (I'll later use that variable to point to an array that will lead to the images for the particular category.) So far, I've been trying to use getLastTrialData to access which key was pressed during the category selection block, and store that info in the global last_input var. But, I can't get the value of last_input to update to the numeric key code for the key pressed. Here's my code; any insights?

<!doctype html>

<html>
<head>
<title>singlestimtest</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="jspsych-4.2/jspsych.js"></script>
<script src="plugins/jspsych-single-stim.js"></script>
<script src="plugins/jspsych-text.js"></script>

<link href="jspsych-4.2/css/jspsych.css" rel="stylesheet" type="text/css">
</head>
<body>
</body>
<script>

var last_input = 10;

var welcome_block = {
type: "text",
text: "In a moment you will select a category. Press any key to continue."
};

var categories_block = {
type: 'single-stim',
stimuli: '<p>A B C D</p>',
is_html: true,
choices: ['a', 'b', 'c', 'd'],
response_ends_trial: true,
};

var get_response = {
type:'single-stim',
stimuli: 'press space to see response',
is_html: true,
choices: [' '],
on_finish: function(data) {
alert('working too!');
last_input = jsPsych.data.getLastTrialData();
return last_input;
}
};

var thankyou_block = {
type: "text",
text: "Thanks for participating" + last_input
};


var experiment = [];
experiment.push(welcome_block);
experiment.push(categories_block);
experiment.push(get_response);
experiment.push(thankyou_block);


jsPsych.init({
experiment_structure: experiment,
on_finish: function() {
jsPsych.data.displayData();

}
});

</script>
</html>

Thanks in advance!
Emma

Josh de Leeuw

unread,
Jan 13, 2016, 8:57:37 AM1/13/16
to emars...@gmail.com, jsPsych
Hi,

If you put jsPsych.data.getLastTrialData() inside the on_finish handler of a trial, then the data you get back will be from the trial that just completed. The on_finish event executes after the trial is complete. Instead, you'll need to get the data from two trials ago. You can do that with:

var alldata = jsPsych.data.getData();
var trialdata = alldata[alldata.length - 2];

Then the key should be available as:

trialdata.key_press

Hope that helps,
Josh

--
You received this message because you are subscribed to the Google Groups "jsPsych" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jspsych+u...@googlegroups.com.
To post to this group, send email to jsp...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jspsych/3a163edc-404b-42f5-986a-b2bcb1e5e126%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages