Created
March 23, 2019 04:11
-
-
Save doraneko94/b48de3f9bc3b15a8009da75895d5e863 to your computer and use it in GitHub Desktop.
Controlling a mouse with a gamepad. This code is optimized for "LOGICOOL gamepad F301r".
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame | |
from pygame.locals import * | |
import time, sys | |
import pyautogui | |
def main(): | |
pyautogui.FAILSAFE = False | |
pygame.init() | |
while True: | |
eventlist = pygame.event.get() | |
key = 0 | |
if len(eventlist) == 0: | |
eventlist = eve0 | |
key = 1 | |
eve0 = eventlist | |
if key == 0: | |
buttonlist = [e for e in eventlist if e.type == pygame.locals.JOYBUTTONDOWN] | |
buttonlist = [e.button for e in buttonlist] | |
#hatlist = [e for e in eventlist if e.type == pygame.locals.JOYHATMOTION] | |
#hatlist = [e.value for e in hatlist] | |
else: | |
buttonlist = [] | |
#hatlist = [] | |
hatlist = [e for e in eventlist if e.type == pygame.locals.JOYHATMOTION] | |
hatlist = [e.value for e in hatlist] | |
joylist = [e for e in eventlist if e.type == pygame.locals.JOYAXISMOTION] | |
joylist0 = [e.value for e in joylist if e.axis == 0] | |
joylist1 = [e.value for e in joylist if e.axis == 1] | |
joylist3 = [e.value for e in joylist if e.axis == 3] | |
if 3 in buttonlist: | |
print("END") | |
sys.exit() | |
if 2 in buttonlist: | |
x, y = pyautogui.position() | |
pyautogui.click(x, y) | |
if len(joylist0) > 0 or len(joylist1) > 0: | |
if len(joylist0) > 0: | |
dx0 = int(joylist0[0]*100) | |
else: | |
dx0 = 0 | |
if len(joylist1) > 0: | |
dy0 = int(joylist1[0]*200) | |
else: | |
dy0 = 0 | |
if abs(dx0) < 5: | |
dx0 = 0 | |
if abs(dy0) < 5: | |
dy0 = 0 | |
pyautogui.moveRel(dx0, dy0) | |
if len(joylist3) > 0: | |
dy = int(joylist3[0]*100)*(-1) | |
pyautogui.scroll(dy) | |
if len(hatlist) > 0: | |
dx, dy = hatlist[0] | |
pyautogui.moveRel(dx*5, dy*(-5)) | |
pygame.time.wait(1) | |
if __name__ == "__main__": | |
pygame.joystick.init() | |
try: | |
joys = pygame.joystick.Joystick(0) | |
joys.init() | |
main() | |
except pygame.error: | |
print("Insert a gamepad!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment