i am writing something which should let me use a joystick as a Spacemouse that i want to use in fusion 360, im doing this by making the mouse move and pressing the middle mouse button and shift, and releasing it when the joystick stops moving, but my problem is that my mouse won't move and klick at the same time and everytime i start the script my pc will go crazy and i'll have to reset it.
This is the code:
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.