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

Metadata

Release files for charz-input 0.1.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for charz-input 0.1.6
File Size Uploaded Provenance
charz_input-0.1.6.tar.gz 6.2 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for charz-input 0.1.6
File Interpreter ABI Platform
charz_input-0.1.6-py3-none-any.whl Python 3 none any Details

Total release size: 14.5 kB

Release files / charz_input-0.1.6.tar.gz

Download URL charz_input-0.1.6.tar.gz
Size 6.2 kB
Tags Source
SHA-256 checksum
How to use checksums
f5c1fca4839497967df6c4b1c9e05cbd3a668856851637cd3aed8878a15c1fd5
BLAKE2b-256 checksum
How to use checksums
863b1b4060f0dd4b3bd95b3d0472aa7682e54dfc6fd20670e81f4b0983ef01d6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.5 {"installer":{"name":"uv","version":"0.13.5","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}

Release files / charz_input-0.1.6-py3-none-any.whl

Download URL charz_input-0.1.6-py3-none-any.whl
Size 8.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bbc5e3ecfe17c8d256c625ef2254a5b7c4237a07ad0adb971138b628dbe88795
BLAKE2b-256 checksum
How to use checksums
8d0da0a2544f251c6ecc1eddcc1ea0c0c886a6a4a02a1f5623bda6353e85b3bd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.5 {"installer":{"name":"uv","version":"0.13.5","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}

Security for charz-input 0.1.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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