1

我正在写一些东西,应该让我将操纵杆用作我想在 fusion 360 中使用的Spacemouse ,我通过使鼠标移动并按下鼠标中键并移动来做到这一点,并在操纵杆停止移动时释放它,但是我的问题是我的鼠标不会同时移动和点击,每次我启动脚本时,我的电脑都会发疯,我必须重置它。

这是代码:

import analogio
import board
import digitalio
import usb_hid
from adafruit_hid.mouse import Mouse
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode

kbd = Keyboard(usb_hid.devices)
mouse = Mouse(usb_hid.devices)
 
x_axis = analogio.AnalogIn(board.A3)
y_axis = analogio.AnalogIn(board.A2)
 
select = digitalio.DigitalInOut(board.A1)
select.direction = digitalio.Direction.INPUT
select.pull = digitalio.Pull.UP

pot_min = 0.00
pot_max = 3.29
step = (pot_max - pot_min) / 20.0
 
 
def get_voltage(pin):
    return (pin.value * 5) / 65536
 
 
def steps(axis):
    """ Maps the potentiometer voltage range to 0-20 """
    return round((axis - pot_min) / step)
 
 
while True:
    #print(str(digitalio.DigitalInOut.value))
    x = get_voltage(x_axis)
    y = get_voltage(y_axis)
    
 
    if select.value is False:
        print("test")
        

        

    if steps(x) > 20.0:
        # print(steps(x))
        mouse.move(x=1)
    if steps(x) < 9.0:
        # print(steps(x))
        mouse.move(x=-1)
 
    if steps(x) > 25.0:
        # print(steps(x))
        mouse.move(x=8)
    if steps(x) < 1.0:
        # print(steps(x))
        mouse.move(x=-8)
 
    if steps(y) > 20.0:
        # print(steps(y))
        mouse.move(y=1)
    if steps(y) < 9.0:
        # print(steps(y))
        mouse.move(y=-1)
 
    if steps(y) > 25.0:
        # print(steps(y))
        mouse.move(y=8)
    if steps(y) < 1.0:
        # print(steps(y))
        mouse.move(y=-8)

    while steps(x) > 20.0 or steps(x) < 9.0 or steps(x) > 25.0 or steps(x) < 1.0 or steps(y) > 20.0 or steps(y) < 9.0 or steps(y) > 25.0 or steps(y) < 1.0:
        kbd.press(Keycode.SHIFT)
        mouse.press(Mouse.MIDDLE_BUTTON)

    else:
        kbd.release(Keycode.SHIFT)
        mouse.release(Mouse.MIDDLE_BUTTON)```

I hope anyone can help me with this.

        


4

1 回答 1

0

找到了答案,因为 reddit 上有人帮助了我,我做了一些功能,不得不重新检查操纵杆的位置:

import time
import analogio
import board
import digitalio
import usb_hid
from adafruit_hid.mouse import Mouse
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode

kbd = Keyboard(usb_hid.devices)
mouse = Mouse(usb_hid.devices)
x_axis = analogio.AnalogIn(board.A3)
y_axis = analogio.AnalogIn(board.A2)
select = digitalio.DigitalInOut(board.A1)
select.direction = digitalio.Direction.INPUT
select.pull = digitalio.Pull.UP

pot_min = 0.00
pot_max = 3.29
step = (pot_max - pot_min) / 20.0

def get_voltage(pin):
    return (pin.value * 5) / 65536

def steps(axis):
    """ Maps the potentiometer voltage range to 0-20 """
    return round((axis - pot_min) / step)

def is_joystick_active( x, y ):
    """Checks whether the joystick is away from the center position"""
    return steps(x) > 21.0 or steps(x) < 9.0 or steps(x) > 25.0 or steps(x) < 1.0 or steps(y) > 21.0 or steps(y) < 9.0 or steps(y) > 25.0 or steps(y) < 1.0

def get_mouse_move( x, y ):
    """Translates x,y voltages into how far to move the mouse on each step"""
    mouse_x = 0;
    mouse_y = 0;
    if steps(x) > 21.0:
        # print(steps(x))
        mouse_x+=1

    if steps(x) < 9.0:
        # print(steps(x))
        mouse_x-=1

    if steps(x) > 25.0:
        # print(steps(x))
        mouse_x+=8

    if steps(x) < 1.0:
        # print(steps(x))
        mouse_x-=8

    if steps(y) > 21.0:
        # print(steps(y))
        mouse_y+=1

    if steps(y) < 9.0:
        # print(steps(y))
        mouse_y-=1

    if steps(y) > 25.0:
        # print(steps(y))
        mouse_y+=8

    if steps(y) < 1.0:
        # print(steps(y))
        mouse_y-=8
    return mouse_x, mouse_y

while True:
    #print(str(digitalio.DigitalInOut.value))
    x = get_voltage(x_axis)
    y = get_voltage(y_axis)

    print("x=" + str(steps(x)))
    print("y=" + str(steps(y)))

    time.sleep(0.5)

    if is_joystick_active( x, y ):
        kbd.press(Keycode.SHIFT)
        mouse.press(Mouse.MIDDLE_BUTTON)

        while is_joystick_active( x, y ):
            mouse_x, mouse_y = get_mouse_move( x, y )
            mouse.move(x=mouse_x, y=mouse_y)
            # Wait for a bit
            time.sleep(0.1)
            # Check the joystick position again
            x = get_voltage(x_axis)
            y = get_voltage(y_axis)
        else:
            kbd.release(Keycode.SHIFT)
            mouse.release(Mouse.MIDDLE_BUTTON)```
于 2022-01-18T16:36:34.780 回答