繁体   English   中英

使用Python读取操纵杆值

[英]Reading joystick values with Python

我想阅读带有Raspberry Pi的Logitech Logitech Extreme 3D Pro的值。 我正在使用pygame库

剧本:

import pygame
import sys
import time

pygame.joystick.init()

print pygame.joystick.get_count()

_joystick = pygame.joystick.Joystick(0)
_joystick.init()
print _joystick.get_init()
print _joystick.get_id()
print _joystick.get_name()
print _joystick.get_numaxes()
print _joystick.get_numballs()
print _joystick.get_numbuttons()
print _joystick.get_numhats()
print _joystick.get_axis(0)

输出:

1
1
0
Logitech Logitech Extreme 3D Pro
4
0
12
SDL_JoystickNumHats value:1:
1
SDL_JoystickGetAxis value:0:
0.0

有4个轴,我全部都转了。

我找不到问题。 我已经尝试过使用其他轴。

感谢帮助。

如果问题在于该值始终为0,请尝试在读取值之前先执行pygame.event.pump() 我遇到了类似的问题,这有所帮助。

我遇到了同样的问题。 您必须编写pygame.event.get()才能从操纵杆读取信息。 否则,它将永远不会更新。

我宁愿等待(甚至在一个线程中)例如:

    axes = [ 0.0 ] * your_joystick.get_numaxes()
    buttons = [ False ] * your_joystick.get_numbuttons()

    while self.keep_alive:
        event = pygame.event.wait()
        if event.type == pygame.QUIT:
             self.keep_alive = False
        elif event.type == pygame.JOYAXISMOTION:
            e = event.dict
            axes[e['axis']] = e['value']
        elif event.type in [pygame.JOYBUTTONUP, pygame.JOYBUTTONDOWN ]:
            e = event.dict
            buttons[e['button']] ^= True

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM