import time
import pygame
import numpy as np
from pynput import mouse
pygame.init()
joystick = pygame.joystick.Joystick(0)
joystick.init()
m = mouse.Controller()
ratio = 30. # 摇杆灵敏度
scroll_speed = 5.0 # 滚轮速度
left_axis_x, left_axis_y = 0.0, 0.0
right_axis_x, right_axis_y = 0.0, 0.0
dead_space = 0.03
last_time = time.time()
control_freq = 60.0 # 控制频率
gap_time = 1.0 / control_freq
while True:
# 获取当前鼠标位置
current_mouse_position = list(m.position)
for event in pygame.event.get():
if event.type == pygame.JOYBUTTONDOWN:
if event.button == 0:
print("A键按下")
elif event.button == 1:
print("B键按下")
elif event.button == 6:
m.press(mouse.Button.left)
elif event.button == 7:
m.press(mouse.Button.right)
elif event.button == 13:
m.press(mouse.Button.middle)
else:
print(f"其他键按下: {event.button}")
elif event.type == pygame.JOYBUTTONUP:
if event.button == 6:
m.release(mouse.Button.left)
elif event.button == 7:
m.release(mouse.Button.right)
elif event.button == 13:
m.release(mouse.Button.middle)
left_axis_x = joystick.get_axis(0)
left_axis_y = joystick.get_axis(1)
if abs(left_axis_x) > dead_space:
if left_axis_x > dead_space:
left_axis_x -= dead_space
elif left_axis_x < -dead_space:
left_axis_x += dead_space
current_mouse_position[0] += int(ratio * np.sign(left_axis_x) * np.sqrt(abs(left_axis_x)))
if abs(left_axis_y) > dead_space:
if left_axis_y > dead_space:
left_axis_y -= dead_space
elif left_axis_y < -dead_space:
left_axis_y += dead_space
current_mouse_position[1] += int(ratio * np.sign(left_axis_y) * np.sqrt(abs(left_axis_y)))
m.position = tuple(map(int, current_mouse_position))
right_axis_x = joystick.get_axis(2)
right_axis_y = joystick.get_axis(3)
horizontal_scroll = 0
vertical_scroll = 0
# 右摇杆控制鼠标滚轮
if abs(right_axis_y) > dead_space:
if right_axis_y > dead_space:
right_axis_y -= dead_space
elif right_axis_y < -dead_space:
right_axis_y += dead_space
# 注意: 向下推摇杆通常产生正值,但滚轮向下是负值,所以取反
vertical_scroll = -int(scroll_speed * np.sign(right_axis_y) * np.sqrt(abs(right_axis_y)))
# 可选:添加水平滚动
if abs(right_axis_x) > dead_space:
if right_axis_x > dead_space:
right_axis_x -= dead_space
elif right_axis_x < -dead_space:
right_axis_x += dead_space
horizontal_scroll = int(scroll_speed * np.sign(right_axis_x) * np.sqrt(abs(right_axis_x)))
m.scroll(horizontal_scroll, vertical_scroll) # 垂直滚动
sleep_time = max(0.0, gap_time - (time.time() - last_time))
time.sleep(sleep_time)
last_time = time.time()
使用手柄模拟鼠标
最新推荐文章于 2025-12-20 15:30:24 发布
部署运行你感兴趣的模型镜像
您可能感兴趣的与本文相关的镜像
Python3.10
Conda
Python
Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本
3236

被折叠的 条评论
为什么被折叠?



