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.5.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.5-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: charz_input-0.1.5.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.5.tar.gz
Algorithm Hash digest
SHA256 058ded949808ca32a8e02eae18f387f1e90b5e766e2c7dd89f878b567980b7a2
MD5 af7c35192c5c8219ac779990c5d7a55f
BLAKE2b-256 b66459cbc287c6c882570c4360b734517d3409dc3e430e84337dac038a917d3c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: charz_input-0.1.5-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.5-py3-none-any.whl
Algorithm Hash digest
SHA256 08e1a0581ac3b789e19959ce2839447170a562ef3dd495518bc441a15dc67255
MD5 ee660bc7b46081fb01fed0de1c818ece
BLAKE2b-256 a8065c406e235111a60ef3f42dc8299da841d0b19e90b7b82aff2f24287492e9

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.5 This release

2 files

0.1.3

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