Skip to content

topdownengine.controls Reference

KeyboardInputManager

Acts as a keyboard and mouse input reciever.

Attributes:

Name Type Description
keybinds dict[str, int | str]

Stores all keybind data.

non_hold_inputs list[str]

List of which inputs can't be held.

keys ScancodeWrapper

Keys currently pressed.

just_pressed_keys ScancodeWrapper

Keys pressed in the current frame.

Source code in topdownengine\controls.py
class KeyboardInputManager:
    """Acts as a keyboard and mouse input reciever.

    Attributes:
        keybinds (dict[str,int|str]): Stores all keybind data.
        non_hold_inputs (list[str]): List of which inputs can't be held.
        keys (pg.key.ScancodeWrapper): Keys currently pressed.
        just_pressed_keys (pg.key.ScancodeWrapper): Keys pressed in the current frame.
    """

    def __init__(self) -> None:
        "Initialize KeyboardInputManager."
        self.keybinds = {
            "Move Right": pg.K_d,
            "Move Left": pg.K_a,
            "Move Up": pg.K_w,
            "Move Down": pg.K_s,
            "Jump": pg.K_SPACE
        }
        self.non_hold_inputs = []

    def serialize(self) -> dict:
        "Serialize the keybinds."
        return {
            k: pg.key.name(v) if type(v) == int else v
            for k, v in self.keybinds.items()
        }

    def deserialize(self, data: dict) -> None:
        "Deserialize the keybinds into this object."
        for k, v in data.items():
            try:
                self.keybinds[k] = pg.key.key_code(v)
            except ValueError:
                self.keybinds[k] = v

    def get_input(self) -> list[str]:
        """Returns all inputs to execute logic for. MUST be called after pg.event.get().

        Returns:
            list[str]: The inputs to execute logic for.
        """
        self.keys = pg.key.get_pressed()
        self.just_pressed_keys = pg.key.get_just_pressed()
        inputs = []

        for keybind in self.keybinds:
            if self.keybinds[keybind] == "Button 1":
                if pg.mouse.get_just_pressed()[0]: 
                    inputs.append(keybind) 
            elif self.keybinds[keybind] == "Button 3":
                if pg.mouse.get_just_pressed()[2]: 
                    inputs.append(keybind) 
            else:
                if self.keys[self.keybinds[keybind]]: 
                    if keybind in self.non_hold_inputs:
                        if self.just_pressed_keys[self.keybinds[keybind]]:
                            inputs.append(keybind)
                    else:
                        inputs.append(keybind) 

        return inputs

__init__()

Initialize KeyboardInputManager.

Source code in topdownengine\controls.py
def __init__(self) -> None:
    "Initialize KeyboardInputManager."
    self.keybinds = {
        "Move Right": pg.K_d,
        "Move Left": pg.K_a,
        "Move Up": pg.K_w,
        "Move Down": pg.K_s,
        "Jump": pg.K_SPACE
    }
    self.non_hold_inputs = []

deserialize(data)

Deserialize the keybinds into this object.

Source code in topdownengine\controls.py
def deserialize(self, data: dict) -> None:
    "Deserialize the keybinds into this object."
    for k, v in data.items():
        try:
            self.keybinds[k] = pg.key.key_code(v)
        except ValueError:
            self.keybinds[k] = v

get_input()

Returns all inputs to execute logic for. MUST be called after pg.event.get().

Returns:

Type Description
list[str]

list[str]: The inputs to execute logic for.

Source code in topdownengine\controls.py
def get_input(self) -> list[str]:
    """Returns all inputs to execute logic for. MUST be called after pg.event.get().

    Returns:
        list[str]: The inputs to execute logic for.
    """
    self.keys = pg.key.get_pressed()
    self.just_pressed_keys = pg.key.get_just_pressed()
    inputs = []

    for keybind in self.keybinds:
        if self.keybinds[keybind] == "Button 1":
            if pg.mouse.get_just_pressed()[0]: 
                inputs.append(keybind) 
        elif self.keybinds[keybind] == "Button 3":
            if pg.mouse.get_just_pressed()[2]: 
                inputs.append(keybind) 
        else:
            if self.keys[self.keybinds[keybind]]: 
                if keybind in self.non_hold_inputs:
                    if self.just_pressed_keys[self.keybinds[keybind]]:
                        inputs.append(keybind)
                else:
                    inputs.append(keybind) 

    return inputs

serialize()

Serialize the keybinds.

Source code in topdownengine\controls.py
def serialize(self) -> dict:
    "Serialize the keybinds."
    return {
        k: pg.key.name(v) if type(v) == int else v
        for k, v in self.keybinds.items()
    }

MoreKeysPressed

Emulates a pygame ScancodeWrapper where given keys are always pressed.

Attributes:

Name Type Description
wrapper

The base wrapper that keys were added to.

pressed_keys

The keys added to the wrapper.

Source code in topdownengine\controls.py
class MoreKeysPressed:
    """Emulates a pygame ScancodeWrapper where given keys are always pressed.

    Attributes:
        wrapper: The base wrapper that keys were added to.
        pressed_keys: The keys added to the wrapper.
    """

    def __init__(self, wrapper: pg.key.ScancodeWrapper, pressed_keys: set[int]) -> None: 
        """Initialize the MoreKeysPressed object.

        Args:
            wrapper: The wrapper to add keys to.
            pressed_keys: The keys to add to the wrapper.
        """
        self.wrapper = wrapper
        self.pressed_keys = pressed_keys

    def __getitem__(self, key: int) -> bool:
        return self.wrapper[key] or key in self.pressed_keys

__init__(wrapper, pressed_keys)

Initialize the MoreKeysPressed object.

Parameters:

Name Type Description Default
wrapper ScancodeWrapper

The wrapper to add keys to.

required
pressed_keys set[int]

The keys to add to the wrapper.

required
Source code in topdownengine\controls.py
def __init__(self, wrapper: pg.key.ScancodeWrapper, pressed_keys: set[int]) -> None: 
    """Initialize the MoreKeysPressed object.

    Args:
        wrapper: The wrapper to add keys to.
        pressed_keys: The keys to add to the wrapper.
    """
    self.wrapper = wrapper
    self.pressed_keys = pressed_keys

NoKeysPressed

Emulates a pygame ScancodeWrapper where no keys are pressed.

Source code in topdownengine\controls.py
class NoKeysPressed:
    "Emulates a pygame ScancodeWrapper where no keys are pressed."
    def __getitem__(self, key: int) -> bool: 
        return False