1

I am working on someone else's code. The code is written in python and it has dependency of Joystick. I want to emulate joystick with keyboard or mouse but i did not find any relevant help on the web

here are some relevant code

from psychopy.hardware import joystick
from input_handler import JoystickServer
... 
# Make sure one joystick is connected.
joystick.backend = 'pygame'
nJoysticks = joystick.getNumJoysticks()
logging.log(level=logging.EXP, msg='{} joysticks detected'.format(nJoysticks))

if nJoysticks == 0:
    print('There is no joystick connected!')
    core.quit()
else:
    J0 = JoystickServer(0, settings['Joystick0_DeadZone'])
    if nJoysticks > 1:
        J1 = JoystickServer(1, settings['Joystick1_DeadZone'])
    else:
        print('You need two joysticks to play!')
        core.quit()
Mihir Mehta
  • 13,743
  • 3
  • 64
  • 88

1 Answers1

1

give this a try. I'm using it for a task to utilize either mouse or logitech game controller. Essentially you set a new function called checkButtonState() for either mouse or game controller which will call the appropriate function.

def joyCheckButtonState(self):
    return self.getButton(self)

def mouseCheckButtonState(self):
    return self.getPressed(self)[0]

def setButtonPress(bttnType,win=None):

    if bttnType.lower() == 'joystick':

        # First check to make sure joystick is connected
        nJoysticks = joystick.getNumJoysticks()
        # Throw an error if there's no joystick detected
        joystick.backend = 'pygame'
        myBttn = joystick.Joystick(0)
        setattr(myBttn,'checkButtonState',joyCheckButtonState)
    elif bttnType.lower() == 'mouse':
        myBttn = event.Mouse(visible=False,newPos=False,win=win)
        setattr(myBttn, 'checkButtonState', mouseCheckButtonState)
    else:
        print('Error! Unknown button type')

    return myBttn

Now you can call myBttn.getPressed() for either hardware used.

Dharman
  • 30,962
  • 25
  • 85
  • 135
nmark24
  • 13
  • 3