Skip to main content

kikaiken_pycontroller

USB・Bluetooth接続のコントローラ入力状態を取得するシンプルなライブラリです。現在の入力状態のみを扱う、ステートレスな設計になっています。 pygame をバックエンドとして使用しますが、pygame 固有の初期化処理はライブラリ内に隠蔽されています。

インストール

uv add kikaiken-kikaiken_pycontroller

基本的な使い方

Controller を with ブロックで開き、ループ内で read() を呼ぶだけです。

from kikaiken_pycontroller import Controller

with Controller(index=0) as ctrl:
    while True:
        state = ctrl.read()
        print(state.axes.axis0)      # 左スティック X 軸: -1.0 〜 1.0
        print(state.buttons.button0) # ボタン0: True / False
        print(state.hats.hat0)       # 十字キー: (x, y)

API リファレンス

コントローラの検索・接続

接続前にどのコントローラが繋がっているか確認できます。

import kikaiken_pycontroller

# 接続中のコントローラ名一覧
kikaiken_pycontroller.list_controllers()
# => ["Xbox Wireless Controller", "DualSense Wireless Controller"]

# 接続台数を確認したい場合
len(kikaiken_pycontroller.list_controllers())  # => 2

Controller

with ブロックで接続・切断を管理します。複数のコントローラを同時に開くこともできます。

# 1台目
with Controller(index=0) as ctrl:
    print(ctrl.name)   # => "Xbox Wireless Controller"
    state = ctrl.read()

# 2台同時
with Controller(0) as ctrl0, Controller(1) as ctrl1:
    state0 = ctrl0.read()
    state1 = ctrl1.read()

index が接続台数以上の場合は IndexError を送出します。


ControllerState — ctrl.read() の戻り値

read() を呼ぶたびに最新の入力状態が得られます。3つの属性を持ちます。

state = ctrl.read()

state.axes    # アナログ軸 → AxesState
state.buttons # ボタン     → ButtonsState
state.hats    # 十字キー   → HatsState

AxesState — アナログ軸

値の範囲は -1.0 〜 1.0 です。axis0 〜 axis7 のプロパティ、またはインデックス・イテレーションでアクセスできます。

axes = state.axes

# プロパティ・インデックス (どちらも0始まり)
axes.axis0  # => 0.003
axes.axis1  # => -1.0
axes[0]     # => 0.003

# イテレーション・件数
len(axes)           # => 6
list(axes)          # => [0.003, -1.0, 0.0, 0.0, -1.0, -1.0]

コントローラが持たない番号にアクセスすると IndexError を送出します。


ButtonsState — ボタン

値は True(押下中) / False(未押下) です。button0 〜 button19 のプロパティ、またはインデックス・イテレーションでアクセスできます。

buttons = state.buttons

# プロパティ・インデックス (どちらも0始まり)
buttons.button0   # => True
buttons.button1   # => False
buttons[0]        # => True

# イテレーション・件数
len(buttons)      # => 14
list(buttons)     # => [True, False, False, ...]

コントローラが持たない番号にアクセスすると IndexError を送出します。


HatsState — 十字キー(ハット)

値は (x, y) の tuple で、各要素は -1、0、1 のいずれかです。hat0 〜 hat3 のプロパティ、またはインデックス・イテレーションでアクセスできます。

hats = state.hats

# プロパティ・インデックス (どちらも0始まり)
hats.hat0   # => (0, 1)  ← 上
hats[0]     # => (0, 1)

# イテレーション・件数
len(hats)   # => 1
list(hats)  # => [(0, 1)]
(x, y) 方向
(0, 0) ニュートラル
(1, 0) 右
(-1, 0) 左
(0, 1) 上
(0, -1) 下

コントローラが持たない番号にアクセスすると IndexError を送出します。


ユーティリティ

kikaiken_pycontroller.utils.inspect(index=None)

接続中のコントローラの軸・ボタン・ハットの状態をリアルタイムで表示します。どのボタンがどのインデックスに対応しているか確認するときに使います。

from kikaiken_pycontroller.utils import inspect

inspect()        # 複数接続時はインデックスを対話的に選択
inspect(index=0) # インデックスを直接指定

Ctrl+C で終了します。

Metadata

Release files for kikaiken-pycontroller 1.0.1

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

Source distribution (sdist)

Source distribution for kikaiken-pycontroller 1.0.1
File Size Uploaded Provenance Actions
kikaiken_pycontroller-1.0.1.tar.gz 9.5 kB Without provenance Details for kikaiken_pycontroller-1.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for kikaiken-pycontroller 1.0.1
File Interpreter ABI Platform Actions
kikaiken_pycontroller-1.0.1-py3-none-any.whl Python 3 none any Details for kikaiken_pycontroller-1.0.1-py3-none-any.whl

Total release size: 18.8 kB

Release files / kikaiken_pycontroller-1.0.1.tar.gz

Download URL kikaiken_pycontroller-1.0.1.tar.gz
Size 9.5 kB
Tags Source
SHA-256 checksum
How to use checksums
d981b0e3f3bb043f4602a42b592a3a64890c632f796da9051a1ae39a18334cf9
BLAKE2b-256 checksum
How to use checksums
f5b2bd01dfda884ea644f7b05b93e7db301759f026a58f5ab243cb9534c7147c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.11 {"installer":{"name":"uv","version":"0.11.11","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / kikaiken_pycontroller-1.0.1-py3-none-any.whl

Download URL kikaiken_pycontroller-1.0.1-py3-none-any.whl
Size 9.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f42654eb97cc41fc6c6b5e715bc03896a997732d340f59cf02561a9ba2cda43b
BLAKE2b-256 checksum
How to use checksums
da4d3c4323d433b29b53482e8c3df32ffff798637b71a24f5efcd6a4701c47f1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.11 {"installer":{"name":"uv","version":"0.11.11","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for kikaiken-pycontroller 1.0.1

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