HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Joystick to mouse/keyboard mapping program

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
joystickprogrammappingkeyboardmouse

Problem

I've been working on a project to map a joystick (in my case Logitech Extreme 3D) to mouse and keyboard so I can use it for web games that don't support gamesticks. My design goals are:

  • Flexibility - I want to be able to modify this for almost any combination of joystick/gamepad to a keyboard/mouse easily.



  • Readability - I'd like to be able to publish this so others can use it without much trouble.



Here is my code:

```
import pygame
from pygame.locals import *
import ctypes
import math

def main():
print('Python Slither controller is ready.')
pygame.init()
clock = pygame.time.Clock()
joysticks = []
for i in range(0, pygame.joystick.get_count()):
joysticks.append(pygame.joystick.Joystick(i))
joysticks[-1].init()

while 1:
clock.tick(60)

updateData(pygame.joystick.Joystick(0))
doActions()

for event in pygame.event.get():
if event.type == QUIT:
print ("Received event 'Quit', exiting.")
return

controls = {'trigger': 0,
'x-axis': 0,
'y-axis': 0,
'z-axis': 0,
'hat-x': 0,
'hat-y': 0,
'slider': 0,
'button-2': 0,
'button-3': 0,
'button-4': 0,
'button-5': 0,
'button-6': 0,
'button-7': 0,
'button-8': 0,
'button-9': 0,
'button-10': 0,
'button-11': 0,
'button-12': 0}

old_controls = controls.copy()

def updateData(joystick):
global old_controls
old_controls = controls.copy()

controls['trigger'] = joystick.get_button(0)
for i in range(1, 12):
controls['button-'+str(i+1)] = joystick.get_button(i)
controls['slider'] = joystick.get_axis(2)
controls['x-axis'] = joystick.get_axis(0)
controls['y-axis'] = joystick.get_axis(1)
controls['z-axis'] = joystick.get_axis(3)
controls['hat-x'] = joystick.get_hat(0)[0]
controls['hat-y'] = joystick.ge

Solution

I really like this in the main loop:

updateData(pygame.joystick.Joystick(0))
      doActions()


Beautifully concise.

You might continue with that theme by doing Extract Helper to produce def look_for_quit_event():

DRY.
The controls assignment is a bit verbose,
and could happen further down in the code.
Prefer:

controls = { key: 0
             for key in actions }


The keys assignment is needlessly opaque:

keys = {'f': 0x46,
        'esc': 0x1B,
        'q': 0x51,
        '.': 0xBE,
        'tab': 0x09,
        'w': 0x57}


Please prefer this.

keys = {ch: ord(ch) for ch in "fqw."}
keys["tab"] = 9
keys["esc"] = 0x1B


Thank you for the various MSDN URLs, they're very helpful.

Code Snippets

updateData(pygame.joystick.Joystick(0))
      doActions()
controls = { key: 0
             for key in actions }
keys = {'f': 0x46,
        'esc': 0x1B,
        'q': 0x51,
        '.': 0xBE,
        'tab': 0x09,
        'w': 0x57}
keys = {ch: ord(ch) for ch in "fqw."}
keys["tab"] = 9
keys["esc"] = 0x1B

Context

StackExchange Code Review Q#151975, answer score: 2

Revisions (0)

No revisions yet.