Поэтому я пытаюсь создать программу, чтобы 2 человека могли играть с 2 клавиатурами на экране одного человека (для старых игр без локальной сети или многопользовательской онлайн-игры, но с 1 компьютерным мультиплеером)
, пока она работает нормально, но у меня все еще есть проблема при отправке специальных символов (таких как shift, esc, ctrl и т. д.).Хост-компьютер получает символ, но не знает, как его нажать.
Код хоста компьютера:
import socket
from pynput.keyboard import Key, Controller
import os
LISTEN_PORT = 700
keyboard = Controller()
def main():
# Create a TCP/IP socket
listening_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ('', LISTEN_PORT)
listening_sock.bind(server_address)
# Listen for incoming connections
listening_sock.listen(1)
# Create a new conversation socket
client_soc, client_address = listening_sock.accept()
# Receiving data from the client
client_msg = client_soc.recv(1024)
client_msg = client_msg.decode()
# Sending data back
msg = "connected!_s"
client_soc.sendall(msg.encode())
print(client_msg)
old_key = 0
while(client_msg != ""):
client_msg = client_soc.recv(1024)
client_msg = client_msg.decode()
print(client_msg)
if(len(client_msg) >= 3 and client_msg[0:3] == "res"):
if("<" in client_msg):
keyboard.release(chr(int(client_msg[client_msg.find("<") + 1:client_msg.find(">")])))
else:
keyboard.release(client_msg[5])
if("<" not in client_msg and len(client_msg) == 3):
keyboard.press(client_msg[1])
elif("<" in client_msg):
keyboard.press(chr(int(client_msg[client_msg.find("<") + 1:client_msg.find(">")])))
# Closing the conversation socket
client_soc.close()
# Closing the listening socket
listening_sock.close()
main()
Код компьютера игрока 2:
import socket
from pynput.keyboard import Key, Controller, Listener
import sys
import readchar
import threading
SERVER_IP = "127.0.0.1"
SERVER_PORT = 700
keyboard = Controller()
my_key = 0
def listen():
# Collect events until released
with Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()
def on_press(key):
global my_key
#print('{0} pressed'.format(key))
my_key = key
my_key = repr(my_key)
if("<" in my_key):
my_key = my_key[my_key.find("<") + 1:-1]
my_key = my_key[my_key.find("<"):]
def on_release(key):
global my_key
#print('{0} release'.format(key))
my_key = "res"
if key == Key.esc:
# Stop listener
return False
threads = []
t = threading.Thread(target=listen)
threads.append(t)
t.start()
def main():
global my_key
old_key = 0
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = (SERVER_IP, SERVER_PORT)
sock.connect(server_address)
# Sending data to server
msg = "connected!_client"
sock.sendall(msg.encode())
# Receiving data from the server
server_msg = sock.recv(1024)
server_msg = server_msg.decode()
print(server_msg)
last_msg = 0
new_msg = 0
inp = ""
while(my_key != "0"):
if(old_key != my_key):
if(my_key == "res"):
inp = my_key + old_key
new_msg = inp.encode()
else:
inp = my_key
new_msg = inp.encode()
print(my_key)
old_key = my_key
sock.sendall(new_msg)
# Closing the socket
sock.close()
main()