Skip to main content

USB・Bluetooth接続のコントローラ入力状態を取得するシンプルなライブラリ

Project description

kikaiken_pycontroller

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

インストール

uv add kikaiken-kikaiken_pycontroller

基本的な使い方

Controllerwith ブロックで開き、ループ内で 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 を送出します。


ControllerStatectrl.read() の戻り値

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

state = ctrl.read()

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

AxesState — アナログ軸

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

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(未押下) です。button0button19 のプロパティ、またはインデックス・イテレーションでアクセスできます。

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 で、各要素は -101 のいずれかです。hat0hat3 のプロパティ、またはインデックス・イテレーションでアクセスできます。

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 で終了します。

Project details


Download files

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

Source Distribution

kikaiken_pycontroller-0.4.1.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

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

kikaiken_pycontroller-0.4.1-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file kikaiken_pycontroller-0.4.1.tar.gz.

File metadata

  • Download URL: kikaiken_pycontroller-0.4.1.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using 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}

File hashes

Hashes for kikaiken_pycontroller-0.4.1.tar.gz
Algorithm Hash digest
SHA256 1a44f33ddbb5a6d22203cd991a5a2c73072a0b5fa7eed18779fac19dff3ceb75
MD5 89019cbc63ac14e5cd1c1cb680da7532
BLAKE2b-256 a38ea057d4a45643d2f185ea132b7506f1b86615d6e372e0d7253512ba1212a0

See more details on using hashes here.

File details

Details for the file kikaiken_pycontroller-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: kikaiken_pycontroller-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using 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}

File hashes

Hashes for kikaiken_pycontroller-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5e0f536d43ee4e58f83cd6774c72b433ea374518cb09c8a3125ba272a6b09038
MD5 a1bcd63497f59ec459507a866843f277
BLAKE2b-256 f8ee8447fdb3f850dcdde1956fb0669b460a45e620bc7619556e8f318fdf175d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page