[英]Reading Joystick in Windows without Pygame using Python
在搜索了幾個小時之后,我似乎找不到使用 Python 在 Windows 中讀取操縱桿軸和按鈕的方法。 我不想使用 Pygame,因為它有巨大的開銷和大小。 此外,我能找到的唯一代碼只能讀取 Xbox 控制器。 我試過使用 XInput dll,但它無法識別操縱桿。
如果有人能指出我使用哪個庫的正確方向,那就太好了。
開銷是一回事,另一回事是pygame要求窗口具有焦點。 這對我來說是一個真正的表演。
這是使用pywinusb的示例:
import pywinusb.hid as hid
from time import sleep
from msvcrt import kbhit
def sample_handler(data):
print("Raw data: {0:}".format(data))
devices = hid.HidDeviceFilter().get_devices()
print(' i par ser ven prd ver name')
for i, dev in enumerate(devices):
print("{0:> 3} {1:> 2} {2:>9} {3:04X} {4:04X} {5:04X} {6:}"
.format(i, dev.parent_instance_id, dev.serial_number, dev.vendor_id,
dev.product_id, dev.version_number, dev.product_name))
selection = int(input("\nEnter number to select device (0-{})\n".format(i)))
device = devices[selection]
print("You have selected {}".format(device.product_name))
try:
device.open()
# set custom raw data handler
device.set_raw_data_handler(sample_handler)
print("\nWaiting for data...\nPress any (system keyboard) key to stop...")
while not kbhit() and device.is_plugged():
# just keep the device opened to receive events
sleep(0.5)
finally:
device.close()
如果這不適合您的目的,您可能想看看這里: https ://gist.github.com/rdb/8883307
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.