Skip to content

Keypad

An object represeting a 16-key keypad of a standard keypad.

Source code in core/keypad.py
class Keypad:
    """
    An object represeting a 16-key keypad of a standard keypad.
    """

    __slots__ = ("state",)

    def __init__(self) -> None:
        """
        Keypad Constructor.

        Attributes:
            state: State storing KEYUP and KEYDOWN status of the 16 keys.
        """
        self.state: t.List[int] = [0] * 16

    @property
    def keymap(self) -> t.Mapping[int, int]:
        """
        Mapping of pygame keys with locations on the keypad.
        """
        return {
            pg.K_1: 1,
            pg.K_2: 2,
            pg.K_3: 3,
            pg.K_4: 12,
            pg.K_q: 4,
            pg.K_w: 5,
            pg.K_e: 6,
            pg.K_r: 13,
            pg.K_a: 7,
            pg.K_s: 8,
            pg.K_d: 9,
            pg.K_f: 14,
            pg.K_z: 10,
            pg.K_x: 0,
            pg.K_c: 11,
            pg.K_v: 15,
        }

    def set(self, index: int) -> None:
        """
        Method to set a key in state array.

        Args:
            index: The key to set.
        """
        self.state[index] = 1

    def unset(self, index: int) -> None:
        """
        Method to unset a key in state array.

        Args:
            index: The key to unset.
        """
        self.state[index] = 0

    def handle(self, event: Event) -> None:
        """
        Handler for KEYDOWN.

        Args:
            event: A pygame Event.
        """
        if event.key in self.keymap:
            key = self.keymap[event.key]
            self.set(key)
            return key

keymap: Mapping[int, int] property readonly +

Mapping of pygame keys with locations on the keypad.

__init__(self) special +

Keypad Constructor.

Attributes:

Name Type Description
state

State storing KEYUP and KEYDOWN status of the 16 keys.

Source code in core/keypad.py
def __init__(self) -> None:
    """
    Keypad Constructor.

    Attributes:
        state: State storing KEYUP and KEYDOWN status of the 16 keys.
    """
    self.state: t.List[int] = [0] * 16

set(self, index) +

Method to set a key in state array.

Parameters:

Name Type Description Default
index int

The key to set.

required
Source code in core/keypad.py
def set(self, index: int) -> None:
    """
    Method to set a key in state array.

    Args:
        index: The key to set.
    """
    self.state[index] = 1

unset(self, index) +

Method to unset a key in state array.

Parameters:

Name Type Description Default
index int

The key to unset.

required
Source code in core/keypad.py
def unset(self, index: int) -> None:
    """
    Method to unset a key in state array.

    Args:
        index: The key to unset.
    """
    self.state[index] = 0

handle(self, event) +

Handler for KEYDOWN.

Parameters:

Name Type Description Default
event Event

A pygame Event.

required
Source code in core/keypad.py
def handle(self, event: Event) -> None:
    """
    Handler for KEYDOWN.

    Args:
        event: A pygame Event.
    """
    if event.key in self.keymap:
        key = self.keymap[event.key]
        self.set(key)
        return key