Skip to content

sweetbean.extension.TouchButton

TouchButton

A class to create touch buttons as input instead of key presses (https://github.com/jspsych/jspsych-contrib/tree/main/packages/extension-touchscreen-buttons)

Source code in sweetbean/extension/TouchButton.py
 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
56
57
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
class TouchButton:
    """
    A class to create touch buttons as input instead of key presses
    (https://github.com/jspsych/jspsych-contrib/tree/main/packages/extension-touchscreen-buttons)
    """

    def __init__(self, key, layout):
        """
        Arguments:
            key: the key that is emulated by the touch button
            layout: the layout of the touch button
        """
        self.key = key
        self.layout = layout

    def to_js(self):
        return f'"{self.key}"'

    def __eq__(self, other):
        return self.key == other.key

    def __hash__(self):
        return hash(self.key)

    @classmethod
    def left(cls, color=None):
        """
        Convenience Function to create a touch button on the left side of the screen
        """
        if color is None:
            color = "#fff"
        return cls(key="l", layout={"key": "l", "color": color, "preset": "left"})

    @classmethod
    def right(cls, color=None):
        """
        Convenience Function to create a touch button on the right side of the screen
        """
        if color is None:
            color = "#fff"
        return cls(key="r", layout={"key": "r", "color": color, "preset": "right"})

    @classmethod
    def bottom_left(cls, color=None):
        """
        Convenience Function to create a touch button on the bottom left side of the screen
        """
        if color is None:
            color = "#fff"
        return cls(
            key="l", layout={"key": "l", "color": color, "preset": "bottom_left"}
        )

    @classmethod
    def bottom_right(cls, color=None):
        """
        Convenience Function to create a touch button on the bottom right side of the screen
        """
        if color is None:
            color = "#fff"
        return cls(
            key="r", layout={"key": "r", "color": color, "preset": "bottom_right"}
        )

    @classmethod
    def top_left(cls, color=None):
        """
        Convenience Function to create a touch button on the top left side of the screen
        """
        if color is None:
            color = "#fff"
        return cls(key="l", layout={"key": "l", "color": color, "preset": "top_left"})

    @classmethod
    def top_right(cls, color=None):
        """
        Convenience Function to create a touch button on the top right side of the screen
        """
        if color is None:
            color = "#fff"
        return cls(key="r", layout={"key": "r", "color": color, "preset": "top_right"})

__init__(key, layout)

Parameters:

Name Type Description Default
key

the key that is emulated by the touch button

required
layout

the layout of the touch button

required
Source code in sweetbean/extension/TouchButton.py
12
13
14
15
16
17
18
19
def __init__(self, key, layout):
    """
    Arguments:
        key: the key that is emulated by the touch button
        layout: the layout of the touch button
    """
    self.key = key
    self.layout = layout

bottom_left(color=None) classmethod

Convenience Function to create a touch button on the bottom left side of the screen

Source code in sweetbean/extension/TouchButton.py
48
49
50
51
52
53
54
55
56
57
@classmethod
def bottom_left(cls, color=None):
    """
    Convenience Function to create a touch button on the bottom left side of the screen
    """
    if color is None:
        color = "#fff"
    return cls(
        key="l", layout={"key": "l", "color": color, "preset": "bottom_left"}
    )

bottom_right(color=None) classmethod

Convenience Function to create a touch button on the bottom right side of the screen

Source code in sweetbean/extension/TouchButton.py
59
60
61
62
63
64
65
66
67
68
@classmethod
def bottom_right(cls, color=None):
    """
    Convenience Function to create a touch button on the bottom right side of the screen
    """
    if color is None:
        color = "#fff"
    return cls(
        key="r", layout={"key": "r", "color": color, "preset": "bottom_right"}
    )

left(color=None) classmethod

Convenience Function to create a touch button on the left side of the screen

Source code in sweetbean/extension/TouchButton.py
30
31
32
33
34
35
36
37
@classmethod
def left(cls, color=None):
    """
    Convenience Function to create a touch button on the left side of the screen
    """
    if color is None:
        color = "#fff"
    return cls(key="l", layout={"key": "l", "color": color, "preset": "left"})

right(color=None) classmethod

Convenience Function to create a touch button on the right side of the screen

Source code in sweetbean/extension/TouchButton.py
39
40
41
42
43
44
45
46
@classmethod
def right(cls, color=None):
    """
    Convenience Function to create a touch button on the right side of the screen
    """
    if color is None:
        color = "#fff"
    return cls(key="r", layout={"key": "r", "color": color, "preset": "right"})

top_left(color=None) classmethod

Convenience Function to create a touch button on the top left side of the screen

Source code in sweetbean/extension/TouchButton.py
70
71
72
73
74
75
76
77
@classmethod
def top_left(cls, color=None):
    """
    Convenience Function to create a touch button on the top left side of the screen
    """
    if color is None:
        color = "#fff"
    return cls(key="l", layout={"key": "l", "color": color, "preset": "top_left"})

top_right(color=None) classmethod

Convenience Function to create a touch button on the top right side of the screen

Source code in sweetbean/extension/TouchButton.py
79
80
81
82
83
84
85
86
@classmethod
def top_right(cls, color=None):
    """
    Convenience Function to create a touch button on the top right side of the screen
    """
    if color is None:
        color = "#fff"
    return cls(key="r", layout={"key": "r", "color": color, "preset": "top_right"})