Skip to content

sweetbean.stimulus.HtmlKeyboardResponse

Blank

Bases: HtmlKeyboardResponse

shows a blank screen

Source code in sweetbean/stimulus/HtmlKeyboardResponse.py
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
class Blank(HtmlKeyboardResponse):
    """
    shows a blank screen
    """

    def __init__(
        self,
        duration=None,
        choices=None,
        correct_key="",
        side_effects=None,
    ):
        """
        Arguments:
            duration: time in ms the stimulus is presented
            choices: the keys that will be recorded if pressed
            correct_key: the correct key to press
            side_effects: Optional side-effect configuration passed to the runtime. This expects
                a list of SideEffect definitions (see SweetBean docs) which can be
                used to update global data like overall score or trial counter.
        """
        super().__init__(
            duration=duration,
            stimulus="",
            choices=choices,
            correct_key=correct_key,
            side_effects=side_effects,
        )

__init__(duration=None, choices=None, correct_key='', side_effects=None)

Parameters:

Name Type Description Default
duration

time in ms the stimulus is presented

None
choices

the keys that will be recorded if pressed

None
correct_key

the correct key to press

''
side_effects

Optional side-effect configuration passed to the runtime. This expects a list of SideEffect definitions (see SweetBean docs) which can be used to update global data like overall score or trial counter.

None
Source code in sweetbean/stimulus/HtmlKeyboardResponse.py
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
def __init__(
    self,
    duration=None,
    choices=None,
    correct_key="",
    side_effects=None,
):
    """
    Arguments:
        duration: time in ms the stimulus is presented
        choices: the keys that will be recorded if pressed
        correct_key: the correct key to press
        side_effects: Optional side-effect configuration passed to the runtime. This expects
            a list of SideEffect definitions (see SweetBean docs) which can be
            used to update global data like overall score or trial counter.
    """
    super().__init__(
        duration=duration,
        stimulus="",
        choices=choices,
        correct_key=correct_key,
        side_effects=side_effects,
    )

Feedback

Bases: Text

show the word correct or incorrect dependent on a correct response to a stimulus before

Source code in sweetbean/stimulus/HtmlKeyboardResponse.py
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
class Feedback(Text):
    """
    show the word correct or incorrect dependent on a correct response to a stimulus before
    """

    def __init__(
        self,
        duration=None,
        correct_message="Correct!",
        false_message="False!",
        correct_color="green",
        false_color="red",
        window=1,
        side_effects=None,
    ):
        """
        Arguments:
            duration: time in ms the stimulus is presented
            correct_message: the message to show if the response was correct
            false_message: the message to show if the response was false
            correct_color: the color of the message if the response was correct
            false_color: the color of the message if the response was false
            window: how far back is the stimulus to check
                    (that stimulus needs to have a choice and a correct_key parameter)
            side_effects: Optional side-effect configuration passed to the runtime. This expects
                a list of SideEffect definitions (see SweetBean docs) which can be
                used to update global data like overall score or trial counter.
        """
        correct = DataVariable("correct", window)

        feedback_txt = FunctionVariable(
            "feedback_txt",
            lambda c_msg, f_msg, cor: c_msg if cor else f_msg,
            [correct_message, false_message, correct],
        )

        feedback_color = FunctionVariable(
            "feedback_color",
            lambda c_color, f_color, cor: c_color if cor else f_color,
            [correct_color, false_color, correct],
        )

        super().__init__(
            duration,
            feedback_txt,
            feedback_color,
            choices=[],
            correct_key="",
            side_effects=side_effects,
        )

__init__(duration=None, correct_message='Correct!', false_message='False!', correct_color='green', false_color='red', window=1, side_effects=None)

Parameters:

Name Type Description Default
duration

time in ms the stimulus is presented

None
correct_message

the message to show if the response was correct

'Correct!'
false_message

the message to show if the response was false

'False!'
correct_color

the color of the message if the response was correct

'green'
false_color

the color of the message if the response was false

'red'
window

how far back is the stimulus to check (that stimulus needs to have a choice and a correct_key parameter)

1
side_effects

Optional side-effect configuration passed to the runtime. This expects a list of SideEffect definitions (see SweetBean docs) which can be used to update global data like overall score or trial counter.

None
Source code in sweetbean/stimulus/HtmlKeyboardResponse.py
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
def __init__(
    self,
    duration=None,
    correct_message="Correct!",
    false_message="False!",
    correct_color="green",
    false_color="red",
    window=1,
    side_effects=None,
):
    """
    Arguments:
        duration: time in ms the stimulus is presented
        correct_message: the message to show if the response was correct
        false_message: the message to show if the response was false
        correct_color: the color of the message if the response was correct
        false_color: the color of the message if the response was false
        window: how far back is the stimulus to check
                (that stimulus needs to have a choice and a correct_key parameter)
        side_effects: Optional side-effect configuration passed to the runtime. This expects
            a list of SideEffect definitions (see SweetBean docs) which can be
            used to update global data like overall score or trial counter.
    """
    correct = DataVariable("correct", window)

    feedback_txt = FunctionVariable(
        "feedback_txt",
        lambda c_msg, f_msg, cor: c_msg if cor else f_msg,
        [correct_message, false_message, correct],
    )

    feedback_color = FunctionVariable(
        "feedback_color",
        lambda c_color, f_color, cor: c_color if cor else f_color,
        [correct_color, false_color, correct],
    )

    super().__init__(
        duration,
        feedback_txt,
        feedback_color,
        choices=[],
        correct_key="",
        side_effects=side_effects,
    )

Fixation

Bases: Text

show a white cross in the middle of the screen

Source code in sweetbean/stimulus/HtmlKeyboardResponse.py
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
class Fixation(Text):
    """
    show a white cross in the middle of the screen
    """

    def __init__(self, duration=None, side_effects=None):
        """
        Arguments:
            duration: time in ms the stimulus is presented
            side_effects: Optional side-effect configuration passed to the runtime. This expects
                a list of SideEffect definitions (see SweetBean docs) which can be
                used to update global data like overall score or trial counter.
        """
        super().__init__(
            duration=duration,
            text="+",
            color="white",
            choices=[],
            correct_key="",
            side_effects=side_effects,
        )

__init__(duration=None, side_effects=None)

Parameters:

Name Type Description Default
duration

time in ms the stimulus is presented

None
side_effects

Optional side-effect configuration passed to the runtime. This expects a list of SideEffect definitions (see SweetBean docs) which can be used to update global data like overall score or trial counter.

None
Source code in sweetbean/stimulus/HtmlKeyboardResponse.py
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
def __init__(self, duration=None, side_effects=None):
    """
    Arguments:
        duration: time in ms the stimulus is presented
        side_effects: Optional side-effect configuration passed to the runtime. This expects
            a list of SideEffect definitions (see SweetBean docs) which can be
            used to update global data like overall score or trial counter.
    """
    super().__init__(
        duration=duration,
        text="+",
        color="white",
        choices=[],
        correct_key="",
        side_effects=side_effects,
    )

Flanker

Bases: Text

show a flanker stimulus (<< < <<; << > <<, >> < >>, ...)

Source code in sweetbean/stimulus/HtmlKeyboardResponse.py
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
class Flanker(Text):
    """
    show a flanker stimulus (<< < <<; << > <<, >> < >>, ...)
    """

    def __init__(
        self,
        duration=None,
        direction="left",
        distractor="left",
        choices=None,
        correct_key="",
        color="white",
        n_flankers=2,
        side_effects=None,
    ):
        """
        Arguments:
            duration: time in ms the stimulus is presented
            direction: the direction of the target (allowed: left, right, l, r, L, R)
            distractor: the direction of the distractor (allowed: left, right, l, r, L, R)
            choices: the keys that will be recorded if pressed
            correct_key: the correct key to press
            color: the color of the text
            n_flankers: the number of distractors
            side_effects: Optional side-effect configuration passed to the runtime. This expects
                a list of SideEffect definitions (see SweetBean docs) which can be
                used to update global data like overall score or trial counter.
        """

        def _txt(dr, dst, n):
            normalized_dir = dr.lower()
            normalized_dist = dst.lower()
            t = ""
            d = ""
            if normalized_dir == "right" or normalized_dir == "r":
                t = ">"
            if normalized_dir == "left" or normalized_dir == "l":
                t = "<"
            if normalized_dist == "right" or normalized_dist == "r":
                d = ">"
            if normalized_dist == "left" or normalized_dist == "l":
                d = "<"
            d = d * n
            return f"{d}{t}{d}"

        txt = FunctionVariable("target", _txt, [direction, distractor, n_flankers])

        super().__init__(
            duration=duration,
            text=txt,
            color=color,
            choices=choices,
            correct_key=correct_key,
            side_effects=side_effects,
        )

__init__(duration=None, direction='left', distractor='left', choices=None, correct_key='', color='white', n_flankers=2, side_effects=None)

Parameters:

Name Type Description Default
duration

time in ms the stimulus is presented

None
direction

the direction of the target (allowed: left, right, l, r, L, R)

'left'
distractor

the direction of the distractor (allowed: left, right, l, r, L, R)

'left'
choices

the keys that will be recorded if pressed

None
correct_key

the correct key to press

''
color

the color of the text

'white'
n_flankers

the number of distractors

2
side_effects

Optional side-effect configuration passed to the runtime. This expects a list of SideEffect definitions (see SweetBean docs) which can be used to update global data like overall score or trial counter.

None
Source code in sweetbean/stimulus/HtmlKeyboardResponse.py
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
def __init__(
    self,
    duration=None,
    direction="left",
    distractor="left",
    choices=None,
    correct_key="",
    color="white",
    n_flankers=2,
    side_effects=None,
):
    """
    Arguments:
        duration: time in ms the stimulus is presented
        direction: the direction of the target (allowed: left, right, l, r, L, R)
        distractor: the direction of the distractor (allowed: left, right, l, r, L, R)
        choices: the keys that will be recorded if pressed
        correct_key: the correct key to press
        color: the color of the text
        n_flankers: the number of distractors
        side_effects: Optional side-effect configuration passed to the runtime. This expects
            a list of SideEffect definitions (see SweetBean docs) which can be
            used to update global data like overall score or trial counter.
    """

    def _txt(dr, dst, n):
        normalized_dir = dr.lower()
        normalized_dist = dst.lower()
        t = ""
        d = ""
        if normalized_dir == "right" or normalized_dir == "r":
            t = ">"
        if normalized_dir == "left" or normalized_dir == "l":
            t = "<"
        if normalized_dist == "right" or normalized_dist == "r":
            d = ">"
        if normalized_dist == "left" or normalized_dist == "l":
            d = "<"
        d = d * n
        return f"{d}{t}{d}"

    txt = FunctionVariable("target", _txt, [direction, distractor, n_flankers])

    super().__init__(
        duration=duration,
        text=txt,
        color=color,
        choices=choices,
        correct_key=correct_key,
        side_effects=side_effects,
    )

HtmlKeyboardResponse

Bases: _KeyboardResponseStimulus

A base class for html stimuli

Source code in sweetbean/stimulus/HtmlKeyboardResponse.py
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class HtmlKeyboardResponse(_KeyboardResponseStimulus):
    """
    A base class for html stimuli
    """

    type = "jsPsychHtmlKeyboardResponse"

    def __init__(
        self,
        duration=None,
        stimulus="",
        choices=None,
        correct_key="",
        side_effects=None,
        fit_to_viewport=None,
        min_rt=None,
    ):
        """
        Arguments:
            duration: time in ms the stimulus is presented
            stimulus: the html string representing that is rendered
            choices: the keys that will be recorded if pressed
            correct_key: the correct key to press
            side_effects: Optional side-effect configuration passed to the runtime. This expects
                a list of SideEffect definitions (see SweetBean docs) which can be
                used to update global data like overall score or trial counter.s
            fit_to_viewport: If True (default), the rendered content is
                CSS-zoomed to fill (but not overflow) the viewport.
                If False, natural layout + scrolling is used. If None,
                the class-level default (`fit_to_viewport = True` for
                most stimuli; False for `InformedConsent`) is honored.
                See AUTHORING.md §"Auto-fit to viewport".
            min_rt: Minimum response time in milliseconds. While > 0,
                a document-level capturing keydown listener swallows
                every key press for the first `min_rt` ms so the
                trial cannot end until the participant has plausibly
                looked at the stimulus. Useful as a low-effort
                guardrail against single-key spamming. Defaults to
                `None` (use the class-level default, normally `0`).
                See AUTHORING.md §"Minimum response time".
        """

        if choices is None:
            choices = []
        super().__init__(locals(), side_effects)

    def _add_special_param(self):
        pass

    def _set_before(self):
        pass

__init__(duration=None, stimulus='', choices=None, correct_key='', side_effects=None, fit_to_viewport=None, min_rt=None)

Parameters:

Name Type Description Default
duration

time in ms the stimulus is presented

None
stimulus

the html string representing that is rendered

''
choices

the keys that will be recorded if pressed

None
correct_key

the correct key to press

''
side_effects

Optional side-effect configuration passed to the runtime. This expects a list of SideEffect definitions (see SweetBean docs) which can be used to update global data like overall score or trial counter.s

None
fit_to_viewport

If True (default), the rendered content is CSS-zoomed to fill (but not overflow) the viewport. If False, natural layout + scrolling is used. If None, the class-level default (fit_to_viewport = True for most stimuli; False for InformedConsent) is honored. See AUTHORING.md §"Auto-fit to viewport".

None
min_rt

Minimum response time in milliseconds. While > 0, a document-level capturing keydown listener swallows every key press for the first min_rt ms so the trial cannot end until the participant has plausibly looked at the stimulus. Useful as a low-effort guardrail against single-key spamming. Defaults to None (use the class-level default, normally 0). See AUTHORING.md §"Minimum response time".

None
Source code in sweetbean/stimulus/HtmlKeyboardResponse.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
def __init__(
    self,
    duration=None,
    stimulus="",
    choices=None,
    correct_key="",
    side_effects=None,
    fit_to_viewport=None,
    min_rt=None,
):
    """
    Arguments:
        duration: time in ms the stimulus is presented
        stimulus: the html string representing that is rendered
        choices: the keys that will be recorded if pressed
        correct_key: the correct key to press
        side_effects: Optional side-effect configuration passed to the runtime. This expects
            a list of SideEffect definitions (see SweetBean docs) which can be
            used to update global data like overall score or trial counter.s
        fit_to_viewport: If True (default), the rendered content is
            CSS-zoomed to fill (but not overflow) the viewport.
            If False, natural layout + scrolling is used. If None,
            the class-level default (`fit_to_viewport = True` for
            most stimuli; False for `InformedConsent`) is honored.
            See AUTHORING.md §"Auto-fit to viewport".
        min_rt: Minimum response time in milliseconds. While > 0,
            a document-level capturing keydown listener swallows
            every key press for the first `min_rt` ms so the
            trial cannot end until the participant has plausibly
            looked at the stimulus. Useful as a low-effort
            guardrail against single-key spamming. Defaults to
            `None` (use the class-level default, normally `0`).
            See AUTHORING.md §"Minimum response time".
    """

    if choices is None:
        choices = []
    super().__init__(locals(), side_effects)

Text

Bases: HtmlKeyboardResponse

Show colored text

Source code in sweetbean/stimulus/HtmlKeyboardResponse.py
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
class Text(HtmlKeyboardResponse):
    """
    Show colored text
    """

    l_template: str = (
        "You see "
        '{% if text %}"{{text}}" in "{{color}}"'
        "{% else %}a blank screen{% endif %}"
        "{% if duration %} for {{duration}}ms{% endif %}."
    )

    def __init__(
        self,
        duration=None,
        text="",
        color="white",
        choices=None,
        correct_key="",
        side_effects=None,
    ):
        """
        Arguments:
            duration: time in ms the stimulus is presented
            text: the text should be presented
            color: the color of the text
            choices: the keys that will be recorded if pressed
            correct_key: the correct key to press
            side_effects: Optional side-effect configuration passed to the runtime. This expects
                a list of SideEffect definitions (see SweetBean docs) which can be
                used to update global data like overall score or trial counter.s
        """
        if choices is None:
            choices = []

        def stim(cl, txt):
            return f"<div style='color:{cl}'>{txt}</div>"

        stimulus_ = FunctionVariable("stimulus", stim, [color, text])
        super().__init__(duration, stimulus_, choices, correct_key, side_effects)

        self.arg.update({"text": text, "color": color})

__init__(duration=None, text='', color='white', choices=None, correct_key='', side_effects=None)

Parameters:

Name Type Description Default
duration

time in ms the stimulus is presented

None
text

the text should be presented

''
color

the color of the text

'white'
choices

the keys that will be recorded if pressed

None
correct_key

the correct key to press

''
side_effects

Optional side-effect configuration passed to the runtime. This expects a list of SideEffect definitions (see SweetBean docs) which can be used to update global data like overall score or trial counter.s

None
Source code in sweetbean/stimulus/HtmlKeyboardResponse.py
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
def __init__(
    self,
    duration=None,
    text="",
    color="white",
    choices=None,
    correct_key="",
    side_effects=None,
):
    """
    Arguments:
        duration: time in ms the stimulus is presented
        text: the text should be presented
        color: the color of the text
        choices: the keys that will be recorded if pressed
        correct_key: the correct key to press
        side_effects: Optional side-effect configuration passed to the runtime. This expects
            a list of SideEffect definitions (see SweetBean docs) which can be
            used to update global data like overall score or trial counter.s
    """
    if choices is None:
        choices = []

    def stim(cl, txt):
        return f"<div style='color:{cl}'>{txt}</div>"

    stimulus_ = FunctionVariable("stimulus", stim, [color, text])
    super().__init__(duration, stimulus_, choices, correct_key, side_effects)

    self.arg.update({"text": text, "color": color})