Skip to main content

Charz Input

Versatile input interfaces, for keyboard and console.

Examples

Defining an Action Enum, with all possible actions as variants:

from enum import Enum, auto


class Action(Enum):
    SWITCH_INPUT = auto()
    MOVE_LEFT = auto()
    MOVE_RIGHT = auto()

Switching between Keyboard and Controller input methods:

# ... defined `Action` enum ...

from charz_input import Input, Keyboard, Controller
import pygame

handler: Input[Action] = Keyboard({
    Action.SWITCH_INPUT: "Space",
    Action.MOVE_LEFT: "A",
    Action.MOVE_RIGHT: "D",
})
player_position_x: int = 0

while True:
    handler.capture_states()

    if handler.is_action_just_pressed(Action.SWITCH_INPUT):
        if isinstance(handler, Keyboard):
            handler = Controller({
                Action.SWITCH_INPUT: pygame.CONTROLLER_BUTTON_A,
                Action.MOVE_LEFT: Controller.Trigger(
                    pygame.CONTROLLER_AXIS_LEFTX,
                    Controller.Trigger.Limit.NEGATIVE,
                ),
                Action.MOVE_RIGHT: Controller.Trigger(
                    pygame.CONTROLLER_AXIS_LEFTX,
                    Controller.Trigger.Limit.POSITIVE,
                    deadzone=35,
                ),
            })
        elif isinstance(handler, Controller):
            handler = Keyboard({
                Action.SWITCH_INPUT: "Space",
                Action.MOVE_LEFT: "A",
                Action.MOVE_RIGHT: "D",
            })
        continue

    if handler.is_action_pressed(Action.MOVE_LEFT):
        player_position_x -= 1
    if handler.is_action_pressed(Action.MOVE_RIGHT):
        player_position_x += 1

    print(f"The player is located at x = {player_position_x}")

Declearing default keybinds:

import charz_input

# ... defined `Action` enum ...


class CustomKeyboard(charz_input.Keyboard):
    default_action_binds = {
        Action.SWITCH_INPUT: "Space",
        Action.MOVE_LEFT: "A",
        Action.MOVE_RIGHT: "D",
    }


handler = CustomKeyboard[Action]()
different_handler = CustomKeyboard({
    Action.MOVE_RIGHT: "Y",  # Overrides keybind
    # NOTE: The rest are set to their defaults,
    # as defined by `default_action_binds`
})

# ... logic to check for action presses ...

You can ensure all actions are mapped at runtime, using a typed class item when instantiating:

                # ┌──────┬─────> Action enum, as class item
handler = Keyboard[Action]({
    Action.SWITCH_INPUT: "Space",
    Action.MOVE_LEFT: "A",
    # Missing `Action.MOVE_RIGHT`
})
handler.capture_states()  # This triggers the check
>> AssertionError: Missing actions

# Annotating the variable, and not the instance, will have different results:
handler: Keyboard[Action] = Keyboard({...})

Checking for keyboard presses with a modifier:

handler = Keyboard[Action](
    # ... keybinds ...
    Action.MOVE_LEFT: "W+{modifier}",  # Dynamic, in the sense of initializing
    Action.MOVE_RIGHT: "W",
    Action.SWITCH_INPUT: "Ctrl+Space"  # Can also be hardcoded
    modifier="Space",  # Default: "Shift"
)

handler.capture_states()
print(handler.is_pressed())
handler.action_binds
handler.capture_states()
print(handler.is_pressed())

Roadmap

  • Expose action binds at runtime, so they can be changed
  • Figure out whether automatic device detection for controller is ideal

License

MIT

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

charz_input-0.1.3.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

charz_input-0.1.3-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file charz_input-0.1.3.tar.gz.

File metadata

  • Download URL: charz_input-0.1.3.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.3 {"installer":{"name":"uv","version":"0.12.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for charz_input-0.1.3.tar.gz
Algorithm Hash digest
SHA256 1cd4a975bce6d32da9d3f34a19188e74ef5b5dae2a4de731bd9f3a6cb85dba61
MD5 ce65c317677b6490f074c970355d91c0
BLAKE2b-256 7555a97a1d5cf284115929628f3bfade0104f5f6906a930d7f8d0e5b4b2f490d

See more details on using hashes here.

File details

Details for the file charz_input-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: charz_input-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.3 {"installer":{"name":"uv","version":"0.12.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for charz_input-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 a30590d8302cad86e6b0a7a84e1e430a538ef2a8d75a2fb8b387bd45e1ab3d4e
MD5 25d0b7b072e3681a75dda79cb2ce4dd1
BLAKE2b-256 36177571004cc06144ac9b9d52c4f7c2c239aafe3425867013f6ac8e195116b3

See more details on using hashes here.

Release history Release notifications | RSS feed

0.1.5

2 files

This release

0.1.3 This release

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page