xbox手柄usb连接linux python控制
手柄在linux中叫joystick 首先插上手柄,打开目录/dev/input
,ls
看到里面以js0
,js1
等文件就是手柄的输入。
用这个代码。
这个代码的效果跟jstest效果一样。就是输出你按下的手柄信息。可以根据自己的需求修改。
import os, struct, array
from fcntl import ioctl
# Iterate over the joystick devices.
print('Available devices:')
for fn in os.listdir('/dev/input'):
if fn.startswith('js'):
print(' /dev/input/%s' % (fn))
# 这句显示手柄在硬件中的端口位置: /dev/input/js0
# We'll store the states here.
axis_states = {
'LX': 0,
'LY': 0,
'RX': 0,
'RY': 0,
'LT': 0,
'RT': 0,
'XX': 0,
'YY': 0,
}
button_states = {
'A': 0,
'B': 0,
'X': 0,
'Y': 0,
'LB': 0,
'RB': 0,
'START': 0,
'BACK': 0,
'HOME': 0,