import requests
from pynput import keyboard

# Replace with the IP address of your Arduino web server
url = 'http://192.168.4.1/'

accept_commands = ['R', 'W', 'A', 'S', 'D']

def on_press(key):
	command = ''
	try:
		if (hasattr(key, 'char') and key.char is not None):
			if (key.char.upper() in accept_commands):
				command = key.char.upper()
		elif (key.value.vk == 126):
			command = 'W'
		elif (key.value.vk == 125):
			command = 'S'
		elif (key.value.vk == 123):
			command = 'A'
		elif (key.value.vk == 124):
			command = 'D'
	except:
		pass
	if (command != ''):
		print(command)
		try:
			response = requests.get(f"{url}?command={command}")
			print(response.text)
		except requests.ConnectionError:
			print("Failed to connect to the server.")
		except Exception as e:
			print(f"An error occurred: {e}")

with keyboard.Listener(on_press=on_press) as listener:
	listener.join()