[英]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.