fly_stick
本页由
fr-docs-dev gen-api从源码 docstring 生成,请勿手工编辑;重新生成执行just api。
FlyStick:Linux evdev 操纵杆的 Python 接口。
对 Rust/PyO3 扩展 fly_stick._core 的公开类型与函数做统一导出,包括设备枚举与状态读取、TOML 设备描述与多设备状态池。
公开成员
| 名称 | 类型 | 定义位置 |
|---|---|---|
DeviceButtonMode | 类 | 本页 |
DeviceDescription | 类 | 本页 |
DeviceItem | 类 | 本页 |
JoystickInfo | 类 | 本页 |
JoystickState | 类 | 本页 |
PyDevicePool | 类 | 本页 |
PyJoystick | 类 | 本页 |
fetch_connected_joysticks | 函数 | 本页 |
定义
DeviceButtonMode
python
class DeviceButtonMode(mode: str)PyDevicePool 的按键触发模式。
定义按键按下的两种登记方式:
trigger:仅在按下的瞬间登记一次,随后立即复位;适合开火、触发事件等每次按下只执行一次的动作。hold:按住期间持续登记为按下;适合需要持续输入的交互。
模式在创建 PyDevicePool 时指定,决定输入处理期间按键事件的语义。
python
from fly_stick import DeviceButtonMode
mode = DeviceButtonMode.trigger()
print(str(mode), mode == DeviceButtonMode.trigger())运算符
| 运算符 | 方法 | 说明 |
|---|---|---|
str() | __str__ | 返回 "Trigger" 或 "Hold"。 |
repr() | __repr__ | 返回 DeviceButtonMode.Trigger 形式的调试字符串。 |
== | __eq__ | 比较两个按键模式是否相同。 |
__init__
python
def __init__(mode: str) -> None按字符串模式构造。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
mode | str | — | 模式字符串,取值为 "trigger" 或 "hold"。 |
异常
| 类型 | 说明 |
|---|---|
ValueError | mode 不是 "trigger" 或 "hold" 时抛出。 |
实现位置:packages/fly_stick/src/fly_stick/_core.pyi:149
trigger
python
def trigger() -> DeviceButtonMode返回 trigger 模式。
返回
| 类型 | 说明 |
|---|---|
DeviceButtonMode | trigger 模式常量。 |
实现位置:packages/fly_stick/src/fly_stick/_core.pyi:160
hold
python
def hold() -> DeviceButtonMode返回 hold 模式。
返回
| 类型 | 说明 |
|---|---|
DeviceButtonMode | hold 模式常量。 |
实现位置:packages/fly_stick/src/fly_stick/_core.pyi:168
实现位置:packages/fly_stick/src/fly_stick/_core.pyi:129
DeviceDescription
python
class DeviceDescription(device_name: str | None = None, author: str | None = None, created: str | None = None, description: str | None = None, axes: list[DeviceItem] | None = None, buttons: list[DeviceItem] | None = None, hats: list[DeviceItem] | None = None, device_path: str | None = None)完整描述一个操纵杆或手柄设备的输入布局。
保存设备元数据(名称、可选设备路径、作者、创建日期、说明)以及全部输入元素(轴、按键、帽开关)。既可从 TOML 文件加载,也可据此构造初始 JoystickState。
属性
| 名称 | 类型 | 说明 |
|---|---|---|
device_name | str | 设备名称;未提供时默认为 "Unknown Device"。 |
device_path | str | None | 可选的 evdev 设备路径;同名设备不唯一时用它消歧。 |
author | str | None | 设备描述的作者。 |
created | str | None | 设备描述的创建日期或时间戳。 |
description | str | None | 设备详细说明。 |
axes | list[DeviceItem] | 轴输入项列表。 |
buttons | list[DeviceItem] | 按键输入项列表。 |
hats | list[DeviceItem] | 帽开关(POV)输入项列表。 |
python
from fly_stick import DeviceDescription
device = DeviceDescription.from_toml("devices/Thrustmaster/ta320.toml")
state = device.build_state()
print(device.device_name, sorted(state.axes))device_name
python
device_name: str实现位置:packages/fly_stick/src/fly_stick/_core.pyi:233
device_path
python
device_path: str | None实现位置:packages/fly_stick/src/fly_stick/_core.pyi:234
author
python
author: str | None实现位置:packages/fly_stick/src/fly_stick/_core.pyi:235
created
python
created: str | None实现位置:packages/fly_stick/src/fly_stick/_core.pyi:236
description
python
description: str | None实现位置:packages/fly_stick/src/fly_stick/_core.pyi:237
axes
python
axes: list[DeviceItem]实现位置:packages/fly_stick/src/fly_stick/_core.pyi:238
buttons
python
buttons: list[DeviceItem]实现位置:packages/fly_stick/src/fly_stick/_core.pyi:239
hats
python
hats: list[DeviceItem]实现位置:packages/fly_stick/src/fly_stick/_core.pyi:240
__init__
python
def __init__(device_name: str | None = None, author: str | None = None, created: str | None = None, description: str | None = None, axes: list[DeviceItem] | None = None, buttons: list[DeviceItem] | None = None, hats: list[DeviceItem] | None = None, device_path: str | None = None) -> None构造设备描述。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
device_name | str | None | None | 设备名称;为 None 时使用 "Unknown Device"。 |
author | str | None | None | 作者。 |
created | str | None | None | 创建日期或时间戳。 |
description | str | None | None | 设备详细说明。 |
axes | list[DeviceItem] | None | None | 轴输入项;为 None 时使用空列表。 |
buttons | list[DeviceItem] | None | None | 按键输入项;为 None 时使用空列表。 |
hats | list[DeviceItem] | None | None | 帽开关输入项;为 None 时使用空列表。 |
device_path | str | None | None | 设备节点路径;为 None 时按唯一名称匹配。 |
实现位置:packages/fly_stick/src/fly_stick/_core.pyi:242
from_toml
python
def from_toml(toml_file: str) -> DeviceDescription从 TOML 文件加载设备描述。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
toml_file | str | — | TOML 配置文件路径。 |
返回
| 类型 | 说明 |
|---|---|
DeviceDescription | 解析得到的设备描述。 |
异常
| 类型 | 说明 |
|---|---|
OSError | 读取文件失败时抛出。 |
ValueError | TOML 内容无法解析为设备描述时抛出。 |
实现位置:packages/fly_stick/src/fly_stick/_core.pyi:266
build_state
python
def build_state() -> JoystickState按输入项构造初始状态。
所有轴、按键与帽开关都置零,用于在收到真实输入前占位。
返回
| 类型 | 说明 |
|---|---|
JoystickState | 含全部已声明输入项且数值为零的状态。 |
实现位置:packages/fly_stick/src/fly_stick/_core.pyi:282
实现位置:packages/fly_stick/src/fly_stick/_core.pyi:206
DeviceItem
python
class DeviceItem(code: int, alias: str | None = None)设备输入项:硬件 code 与可选别名。
属性
| 名称 | 类型 | 说明 |
|---|---|---|
code | int | 硬件输入 code。 |
alias | str | None | 便于阅读的别名;为 None 时仅以 code 标识。 |
code
python
code: int实现位置:packages/fly_stick/src/fly_stick/_core.pyi:194
alias
python
alias: str | None实现位置:packages/fly_stick/src/fly_stick/_core.pyi:195
__init__
python
def __init__(code: int, alias: str | None = None) -> None按 code 与可选别名构造输入项。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
code | int | — | 硬件输入 code。 |
alias | str | None | None | 可选别名。 |
实现位置:packages/fly_stick/src/fly_stick/_core.pyi:197
实现位置:packages/fly_stick/src/fly_stick/_core.pyi:186
JoystickInfo
python
class JoystickInfo(path: str, name: str)单个操纵杆设备的基本信息。
属性
| 名称 | 类型 | 说明 |
|---|---|---|
path | str | 设备节点路径,如 /dev/input/event0。 |
name | str | 设备名称;读取失败时为 "Unknown"。 |
path
python
path: str实现位置:packages/fly_stick/src/fly_stick/_core.pyi:17
name
python
name: str实现位置:packages/fly_stick/src/fly_stick/_core.pyi:18
__init__
python
def __init__(path: str, name: str) -> None构造设备信息。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
path | str | — | 设备节点路径。 |
name | str | — | 设备名称。 |
实现位置:packages/fly_stick/src/fly_stick/_core.pyi:20
实现位置:packages/fly_stick/src/fly_stick/_core.pyi:9
JoystickState
python
class JoystickState()轴、按键与方向帽状态;完整快照或本轮事件差分由读取方法决定。
包含按硬件 code 索引的轴、按键与帽开关(POV)数值。
属性
| 名称 | 类型 | 说明 |
|---|---|---|
axes | dict[int, float] | 轴 code 到归一化数值(约 -1.0 到 1.0)的映射。 |
buttons | dict[int, int] | 按键 code 到按下状态(0 未按下,1 按下)的映射。 |
hats | dict[int, int] | 帽开关 code 到方向值(-1、0 或 1)的映射。 |
运算符
| 运算符 | 方法 | 说明 |
|---|---|---|
== | __eq__ | 比较轴、按键与帽开关是否逐一相等。 |
repr() | __repr__ | 返回含轴、按键与帽开关的调试字符串。 |
axes
python
axes: dict[int, float]实现位置:packages/fly_stick/src/fly_stick/_core.pyi:40
buttons
python
buttons: dict[int, int]实现位置:packages/fly_stick/src/fly_stick/_core.pyi:41
hats
python
hats: dict[int, int]实现位置:packages/fly_stick/src/fly_stick/_core.pyi:42
__init__
python
def __init__() -> None构造三个内部映射均为空的初始状态。
实现位置:packages/fly_stick/src/fly_stick/_core.pyi:44
to_dict
python
def to_dict() -> dict[str, dict[int, float | int]]转换为按硬件 code 索引的嵌套字典。
返回
| 类型 | 说明 |
|---|---|
dict[str, dict[int, float | int]] | dict[str, dict[int, float | int]]: 含 "axes"、"buttons" 与 |
dict[str, dict[int, float | int]] | "hats" 三个键,其值为 code 到数值的字典。 |
实现位置:packages/fly_stick/src/fly_stick/_core.pyi:53
to_alias_dict
python
def to_alias_dict(desc: DeviceDescription) -> dict[str, dict[str, float | int]]转换为按别名索引的嵌套字典。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
desc | DeviceDescription | — | 提供 code 到别名映射的设备描述。 |
返回
| 类型 | 说明 |
|---|---|
dict[str, dict[str, float | int]] | dict[str, dict[str, float | int]]: 含 "axes"、"buttons" 与 |
dict[str, dict[str, float | int]] | "hats" 三个键,其值为别名到数值的字典;没有别名的输入项回退 |
dict[str, dict[str, float | int]] | 为 code 的字符串形式。 |
实现位置:packages/fly_stick/src/fly_stick/_core.pyi:62
get_alias_axes
python
def get_alias_axes(desc: DeviceDescription) -> dict[str, float]按别名取出各轴数值。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
desc | DeviceDescription | — | 提供轴 code 到别名映射的设备描述。 |
返回
| 类型 | 说明 |
|---|---|
dict[str, float] | dict[str, float]: 别名到轴数值的字典。 |
实现位置:packages/fly_stick/src/fly_stick/_core.pyi:77
get_alias_buttons
python
def get_alias_buttons(desc: DeviceDescription) -> dict[str, int]按别名取出各按键数值。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
desc | DeviceDescription | — | 提供按键 code 到别名映射的设备描述。 |
返回
| 类型 | 说明 |
|---|---|
dict[str, int] | dict[str, int]: 别名到按键数值的字典。 |
实现位置:packages/fly_stick/src/fly_stick/_core.pyi:88
get_alias_hats
python
def get_alias_hats(desc: DeviceDescription) -> dict[str, int]按别名取出各帽开关数值。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
desc | DeviceDescription | — | 提供帽开关 code 到别名映射的设备描述。 |
返回
| 类型 | 说明 |
|---|---|
dict[str, int] | dict[str, int]: 别名到帽开关数值的字典。 |
实现位置:packages/fly_stick/src/fly_stick/_core.pyi:99
实现位置:packages/fly_stick/src/fly_stick/_core.pyi:29
PyDevicePool
python
class PyDevicePool(device_descs: dict[str, DeviceDescription], debounce_seconds: float = 0.1, button_mode: DeviceButtonMode = ...)多个操纵杆设备的异步状态池。
按逻辑名称管理一组 DeviceDescription,异步初始化设备、合并各设备的输入事件为完整快照并执行按键按下去抖;状态通过 fetch 系列方法获取。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
device_descs | dict[str, DeviceDescription] | — | 逻辑设备名到设备描述的映射。 |
debounce_seconds | float | 0.1 | 按键去抖间隔,单位秒;默认为 0.1。 |
button_mode | DeviceButtonMode | ... | 按键触发模式,默认为 Hold。 |
python
import asyncio
from fly_stick import DeviceDescription, PyDevicePool
async def main() -> None:
pool = PyDevicePool(
{
"ta320": DeviceDescription.from_toml(
"devices/Thrustmaster/ta320.toml"
),
}
)
await pool.reset()
states = await pool.fetch(timeout_seconds=1.0)
print(states["ta320"].axes)
await pool.stop()
asyncio.run(main())NOTE
设备池不再使用时应调用 await pool.stop(),以释放设备与后台任务。
__init__
python
def __init__(device_descs: dict[str, DeviceDescription], debounce_seconds: float = 0.1, button_mode: DeviceButtonMode = ...) -> None记录设备描述、去抖时长与按键语义模式,设备在 reset() 中打开。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
device_descs | dict[str, DeviceDescription] | — | 逻辑设备名到设备描述的映射。 |
debounce_seconds | float | 0.1 | 按钮按下边沿的去抖时长,单位秒;释放、轴与方向帽 不受去抖影响。 |
button_mode | DeviceButtonMode | ... | 按键语义模式。 |
实现位置:packages/fly_stick/src/fly_stick/_core.pyi:366
reset
python
def reset() -> Future[dict[str, tuple[DeviceDescription, JoystickInfo]]]重新枚举、匹配并预打开全部设备,成功后启动监控。
必须在调用 fetch 系列方法之前等待本方法完成。
返回
| 类型 | 说明 |
|---|---|
Future[dict[str, tuple[DeviceDescription, JoystickInfo]]] | 可等待的 Future;完成结果为逻辑设备名到 (设备描述, 设备信息) 的映射。 |
异常
| 类型 | 说明 |
|---|---|
LookupError | 某个描述没有匹配设备。 |
ValueError | 名称匹配不唯一或配置重复。 |
OSError | 任一设备打开失败;此时整个池保持停止。 |
实现位置:packages/fly_stick/src/fly_stick/_core.pyi:381
fetch_nowait
python
def fetch_nowait() -> dict[str, JoystickState]非阻塞地返回各设备当前完整快照。
本方法有独立读取进度;调用它不会消费 fetch() 等待的变化。
返回
| 类型 | 说明 |
|---|---|
dict[str, JoystickState] | 可等待的 Future;完成结果为逻辑设备名到当前完整快照的映射。 |
异常
| 类型 | 说明 |
|---|---|
RuntimeError | 设备池尚未初始化或已停止时抛出。 |
OSError | 监视设备发生读取故障时抛出,故障会停止整个池。 |
python
states = pool.fetch_nowait()
for name, state in states.items():
print(name, state.axes)实现位置:packages/fly_stick/src/fly_stick/_core.pyi:396
fetch
python
def fetch(timeout_seconds: float | None = None) -> Future[dict[str, JoystickState]]等待状态变化或超时,然后返回各设备当前完整快照。
同一设备池同时只允许一个 fetch() 等待;取消等待会释放该限制。 Trigger 模式下每次按下会对每种读取入口锁存为一次脉冲;多次按下在两次观察之间合并为一个脉冲。fetch() 与 fetch_nowait() 的进度独立。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
timeout_seconds | float | None | None | 超时秒数;None 表示一直等待。 |
返回
| 类型 | 说明 |
|---|---|
Future[dict[str, JoystickState]] | 可等待的 Future;完成结果为逻辑设备名到当前完整快照的映射。 |
异常
| 类型 | 说明 |
|---|---|
RuntimeError | 设备池未运行,或已有另一 fetch() 等待。 |
TimeoutError | 超时。 |
OSError | 监控时设备读失败;整个池停止,需显式调用 reset() 恢复。 |
实现位置:packages/fly_stick/src/fly_stick/_core.pyi:417
stop
python
def stop() -> Future[None]停止设备池并释放设备与后台任务。
返回
| 类型 | 说明 |
|---|---|
Future[None] | 可等待的 Future;全部监控任务退出并释放设备后以 None 完成。 |
NOTE
设备池不再使用时务必等待本方法完成清理。
实现位置:packages/fly_stick/src/fly_stick/_core.pyi:438
debounce_time
python
debounce_time: float当前按键去抖间隔,单位秒。
实现位置:packages/fly_stick/src/fly_stick/_core.pyi:450
devices
python
devices: dict[str, tuple[DeviceDescription, JoystickInfo]]已注册的逻辑设备名到 (设备描述, 设备信息) 的映射。
实现位置:packages/fly_stick/src/fly_stick/_core.pyi:454
button_mode
python
button_mode: DeviceButtonMode当前按键触发模式。
实现位置:packages/fly_stick/src/fly_stick/_core.pyi:458
实现位置:packages/fly_stick/src/fly_stick/_core.pyi:329
PyJoystick
python
class PyJoystick(device_path: str)单个操纵杆设备的高层封装。
打开指定 evdev 设备并记录其轴、按键与帽开关能力,提供非阻塞的事件差分读取;设备生命周期由该对象持有。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
device_path | str | — | 设备节点路径,如 /dev/input/event0。 |
python
from fly_stick import PyJoystick
joystick = PyJoystick("/dev/input/js0")
state = joystick.get_state()
print(state.axes, state.buttons, state.hats)__init__
python
def __init__(device_path: str) -> None打开设备节点并缓存其轴、按键与帽开关能力。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
device_path | str | — | 设备节点路径,如 /dev/input/event0。 |
实现位置:packages/fly_stick/src/fly_stick/_core.pyi:311
get_state
python
def get_state() -> JoystickState非阻塞地读取本批事件差分。
返回
| 类型 | 说明 |
|---|---|
JoystickState | 本次采样的轴、按键与帽开关数值。 |
异常
| 类型 | 说明 |
|---|---|
OSError | 读取设备失败时抛出。 |
实现位置:packages/fly_stick/src/fly_stick/_core.pyi:318
实现位置:packages/fly_stick/src/fly_stick/_core.pyi:292
fetch_connected_joysticks
python
def fetch_connected_joysticks() -> list[JoystickInfo]枚举当前连接的输入设备。
通过 evdev 枚举系统输入设备并保留设备路径与名称;名称读取失败时回退为 "Unknown"。
返回
| 类型 | 说明 |
|---|---|
list[JoystickInfo] | list[JoystickInfo]: 已连接设备的信息列表。 |
python
from fly_stick import fetch_connected_joysticks
for device in fetch_connected_joysticks():
print(device.path, device.name)实现位置:packages/fly_stick/src/fly_stick/_core.pyi:110