From ca8830e68ef122c8516172ba0dcad9f901c14d30 Mon Sep 17 00:00:00 2001
From: vzhang03
Date: Sat, 2 Nov 2024 10:05:32 -0400
Subject: [PATCH] Key press is working, time elapsed since last is working,
only now is weird case at begining and end
---
packages/plugin-spr/src/index.ts | 32 +++++++++++++++++++++-----------
1 file changed, 21 insertions(+), 11 deletions(-)
diff --git a/packages/plugin-spr/src/index.ts b/packages/plugin-spr/src/index.ts
index 9959122b..5af9e84a 100644
--- a/packages/plugin-spr/src/index.ts
+++ b/packages/plugin-spr/src/index.ts
@@ -124,7 +124,7 @@ class SprPlugin implements JsPsychPlugin {
const blank = this.generateBlank(this.structured_reading_string[this.index]);
document.querySelector("p")!.innerHTML = blank;
this.addDataPoint(blank, this.index);
- } else document.querySelector("p")!.innerHTML = this.updateDisplayString(null); // update this, passing null for TS
+ } else document.querySelector("p")!.innerHTML = this.updateDisplayString(); // update this, passing null for TS
}
private endTrial() {
@@ -155,7 +155,7 @@ class SprPlugin implements JsPsychPlugin {
}
this.jsPsych.pluginAPI.getKeyboardResponse({
- callback_function: (info) => this.onSpacebarPress(info),
+ callback_function: (info) => this.onValidKeyPress(info),
valid_responses: trial.choices,
rt_method: "performance",
persist: true,
@@ -200,7 +200,7 @@ class SprPlugin implements JsPsychPlugin {
return res;
}
- private onSpacebarPress(info?: any) {
+ private onValidKeyPress(info?: any) {
var newHtml = "";
// handles logic on whether to display blank or show text using boolean/index
@@ -232,7 +232,7 @@ class SprPlugin implements JsPsychPlugin {
}
// This helper method assists with mode 1 and 2 to keep efficency when updating indicies and the scren
- private updateDisplayString(info?: any): string {
+ private updateDisplayString(info: any = {}): string {
if (this.mode === 1 || this.mode === 2) {
if (this.inner_index === -1) {
// need to update new display string
@@ -248,8 +248,7 @@ class SprPlugin implements JsPsychPlugin {
}
this.current_display_string = new_display_string;
- // this.results.push([this.getElapsed()]);
- this.addDataPoint(this.current_display_string.join(" "), this.index);
+ this.addDataPoint(this.current_display_string.join(" "), this.index, info.key);
} else {
if (this.mode === 1 && this.inner_index > 0) {
this.current_display_string[this.inner_index - 1] =
@@ -270,7 +269,7 @@ class SprPlugin implements JsPsychPlugin {
this.structured_reading_string[this.index][this.inner_index] +
"";
- this.addDataPoint(this.current_display_string.join(" "), this.index);
+ this.addDataPoint(this.current_display_string.join(" "), this.index, info.key);
// this.results[this.results.length - 1].push(
// this.structured_reading_string[this.index][this.inner_index],
// this.getElapsed(),
@@ -294,7 +293,7 @@ class SprPlugin implements JsPsychPlugin {
newHtml = "" + newHtml + "
";
this.displayed = true;
- this.addDataPoint(newHtml, this.index);
+ this.addDataPoint(newHtml, this.index, info.key);
console.log("this is info.key:", info.key, "this is new Html", newHtml);
// this.results.push([this.getElapsed(), newHtml, info.key]); // pushes new list with time passed (time looking at blank)
} else {
@@ -310,7 +309,7 @@ class SprPlugin implements JsPsychPlugin {
"
";
}
- this.addDataPoint(newHtml, this.index);
+ this.addDataPoint(newHtml, this.index, info.key);
// this.results[this.results.length - 1].push(this.getElapsed(), info.key); // pushes second time spent looking at word
}
return newHtml;
@@ -349,13 +348,24 @@ class SprPlugin implements JsPsychPlugin {
return res;
}
- private addDataPoint(stimulus: string, line_number: number) {
+ private getTimeElapsed() {
+ const prev = this.startTime;
+ const now = performance.now();
+ this.startTime = now;
+ return Math.round(now - prev);
+ }
+
+ private addDataPoint(stimulus: string, line_number: number, key?: string) {
const res = {
- time_elapsed: Math.round(performance.now() - this.startTime),
+ time_elapsed: this.getTimeElapsed(),
stimulus: stimulus,
line_number: line_number,
};
+ if (key) {
+ res["key_pressed"] = key; // Add key_pressed if key exists
+ }
+
this.results.push(res);
}
}