Skip to content

Instantly share code, notes, and snippets.

@dgellow
Last active December 20, 2015 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dgellow/6155042 to your computer and use it in GitHub Desktop.
Save dgellow/6155042 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#Description: Permet le contrôle de la souris avec une manette.
#Author: Wrap http://linuxfr.org/users/wrap
# Originally posted at http://linuxfr.org/users/wrap/journaux/un-petit-script-pour-les-utilisateurs-de-manette
import pygame
from pygame.locals import *
import Xlib,time
from Xlib import X, display
sroot=display.Display().screen().root
pygame.init()
nb_joysticks = pygame.joystick.get_count()
disable=0
#Bouton pour limiter la vitesse du pointeur
LIMITER=7
# Association des bouton de la manette a une action
#
# binds= {bouton:(materiel, code), ...}
# bouton: numero du bouton de la manette
# materiel: 0=souris, 1=clavier
# code: keycode de la touche pour un clavier,
# numero du bouton pour une souris
binds={0:(0, 1), # souris bouton de gauche
1:(0, 3), # souris bouton de droite
2:(1, 36), # clavier Echap
3:(1,9), # clavier Entrer
4:(1, 117), # clavier pg suivante
5:(1,112), # clavier pg precedente
6:(1,37), # clavier ctrl
#~ 7:(1,50), # clavier Shift
#~ 9:(1,64), # clavier Alt
}
if nb_joysticks > 0:
mon_joystick = pygame.joystick.Joystick(0)
mon_joystick.init()
#On compte les boutons
nb_boutons = mon_joystick.get_numbuttons()
# Init de la memoire pour les etats des bouton
membstate=map(mon_joystick.get_button,range(nb_boutons))
# Init de la memoire pour l' etats de la croix
memhat=[False,False]
hat=[False,False]
continuer = 1
while continuer:
time.sleep(0.01)
#Màj de l'etats de la manette
ev=pygame.event.get()
#recuperation de l'état des boutons
bstate=map(mon_joystick.get_button,range(nb_boutons))
# Permet de verifier si un changement d'etat a eu lieu entre 2 boucles
bstate_ev=map(lambda c,m:c-m,bstate,membstate)
# Conversion de bstate en decimal pour detecter les conbinaion de touches
bstate_int=sum(map(lambda b:int(mon_joystick.get_button(b))*(2**b),range(nb_boutons)))
# Activation/Desactivation des associations avec les boutons
# L1,L2,R1,R2 (bouton 4,5,6,7) simultanément appuyés
if bstate_int==240 and sum(bstate_ev)!=0 and disable==0:
disable=1
for bn in binds.keys():
act=[Xlib.X.ButtonRelease,Xlib.X.KeyRelease]
dev,code=binds[bn]
Xlib.ext.xtest.fake_input(d['root'],act[dev],code)
elif bstate_int==240 and sum(bstate_ev)!=0 and disable==1:
disable=0
if disable==0:
# Gestion des boutons
if sum(bstate_ev)!=0:
for bn,st in enumerate(bstate_ev):
if st==1 and bn in binds.keys():
act=[Xlib.X.ButtonPress,Xlib.X.KeyPress]
dev,code=binds[bn]
Xlib.ext.xtest.fake_input(sroot.query_pointer()._data['root'],act[dev],code)
if st==-1 and bn in binds.keys():
act=[Xlib.X.ButtonRelease,Xlib.X.KeyRelease]
dev,code=binds[bn]
Xlib.ext.xtest.fake_input(sroot.query_pointer()._data['root'],act[dev],code)
# Limitation de la vitesse du pointeur avec le bouton 9 de la manette
K=1+(bstate[LIMITER]*9)
# Gestion du pointeur avec les analogiques
# +00001 pour compenser l'erreur des analogiques qui
# renvoient des valeurs de -1,00001 à 0.99999
axe0= round((mon_joystick.get_axis(0)+0.00001)*10,0)/K
axe1= round((mon_joystick.get_axis(1)+0.00001)*10,0)/K
axe2=0
axe3=0
#decommenter si 2 analogiques
#axe2= round((mon_joystick.get_axis(2)+0.00001)*10,0)/K
#axe3= round((mon_joystick.get_axis(3)+0.00001)*10,0)/K
if axe0!=0 or axe2!=0:
y=sroot.query_pointer()._data["root_y"]
x=sroot.query_pointer()._data["root_x"]
sroot.warp_pointer(x+int(axe0+axe2),y)
if axe1!=0 or axe3!=0:
y=sroot.query_pointer()._data["root_y"]
x=sroot.query_pointer()._data["root_x"]
sroot.warp_pointer(x,y+int(axe1+axe3))
# Gestion des touche HAUT,BAS,GAUCHE,DROITE du clavier avec
# la croix de la manette
nb_hats=mon_joystick.get_numhats()
if nb_hats>0:
hat=mon_joystick.get_hat(0)
hat_ev=map(lambda hc,hm:hc!=hm,hat,memhat)
d=sroot.query_pointer()._data
if True in hat_ev:
if hat[1] ==1 :
Xlib.ext.xtest.fake_input(d['root'],Xlib.X.KeyPress,111)
elif hat[1] ==-1 :
Xlib.ext.xtest.fake_input(d['root'],Xlib.X.KeyPress,116)
elif hat_ev[1]:
Xlib.ext.xtest.fake_input(d['root'],Xlib.X.KeyRelease, 111)
Xlib.ext.xtest.fake_input(d['root'],Xlib.X.KeyRelease, 116)
if hat[0] ==1 :
Xlib.ext.xtest.fake_input(d['root'],Xlib.X.KeyPress,114)
elif hat[0] ==-1 :
Xlib.ext.xtest.fake_input(d['root'],Xlib.X.KeyPress,113)
elif hat_ev[0]:
Xlib.ext.xtest.fake_input(d['root'],Xlib.X.KeyRelease, 113)
Xlib.ext.xtest.fake_input(d['root'],Xlib.X.KeyRelease, 114)
membstate=bstate
memhat=hat
else:
print("Vous n'avez pas branché de Joystick...")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment