
import signal
import sys
import socket
#import time
from pykeyboard import PyKeyboard #pip install pyuserinput
#for windows also pip install pywin32

import vgamepad as vg #pip install vgamepad       https://pypi.org/project/vgamepad/
gamepads = [vg.VX360Gamepad(),vg.VX360Gamepad()]

keyboard = PyKeyboard()

UDP_IP = "0.0.0.0"
UDP_PORT = 5005

sock = socket.socket(socket.AF_INET, # Internet
                     socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))

#for gamepad: "gp0A" for gamepad 0 button A
#each entry corresponds to consecutive ids
keys=[\
['a','d','w','s','q','e'],\
['j','l','i','k','u','o'],\
['f','h','t','g','r','z'],\
['b','n','m',',','.','-'],\
["gp0L","gp0R","gp0U","gp0D","gp0A","gp0B",]\
]

try:
    while True:
        data, (recip,recport) = sock.recvfrom(1024) # buffer size is 1024 bytes
        print("received message: %s" % data)
        id=data[0]
        action=data[1]
        input=data[2]
        print(" ip=%s port=%s" % (recip,recport))
        print(" ID=%s" % id)
        print(" Action=%s" % action)
        print(" Input=%s" % input)
        print()

        if action==1: #press
            key=keys[id][input]
            print(" Press Key=%s" % key)
            if len(key)==1:
                keyboard.press_key(key)
            elif key.startswith("gp"):
                gpid=int(key[2])
                if key[3]=='L':
                    gamepads[gpid].press_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_DPAD_LEFT)
                if key[3]=='R':
                    gamepads[gpid].press_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_DPAD_RIGHT)
                if key[3]=='U':
                    gamepads[gpid].press_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_DPAD_UP)
                if key[3]=='D':
                    gamepads[gpid].press_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_DPAD_DOWN)
                if key[3]=='A':
                    gamepads[gpid].press_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_A)
                if key[3]=='B':
                    gamepads[gpid].press_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_B)
                if key[3]=='S':
                    gamepads[gpid].press_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_START)
                gamepads[gpid].update()
                
        elif action==2: #release
            key=keys[id][input]
            print(" Release Key=%s" % key)
            if len(key)==1:
                keyboard.release_key(key)
            elif key.startswith("gp"):
                gpid=int(key[2])
                if key[3]=='L':
                    gamepads[gpid].release_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_DPAD_LEFT)
                if key[3]=='R':
                    gamepads[gpid].release_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_DPAD_RIGHT)
                if key[3]=='U':
                    gamepads[gpid].release_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_DPAD_UP)
                if key[3]=='D':
                    gamepads[gpid].release_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_DPAD_DOWN)
                if key[3]=='A':
                    gamepads[gpid].release_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_A)
                if key[3]=='B':
                    gamepads[gpid].release_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_B)
                if key[3]=='S':
                    gamepads[gpid].release_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_START)
                gamepads[gpid].update()
                
except KeyboardInterrupt:
  print("Ctrl-C pressed!")
  sys.exit(0)