无法使用Pynput发送特殊密钥

时间:2018-12-03 10:17:32

标签: python-3.x sockets key special-characters pynput

因此,我正在尝试编写一个程序,以便2个人可以在一个人的屏幕上使用2个键盘进行播放(适用于没有lan或在线多人游戏,但只有1个计算机多人游戏的旧游戏)

到目前为止,它可以正常工作,但是发送特殊字符(例如shift,esc,ctrl等)仍然存在问题。主机获取了字符,但是不知道如何按下它。

计算机主机代码:

Damian Wayne CLEANS for Bruce Wayne
Peter Parker BUILDS for Ben Parker

播放器2的计算机代码:

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()

0 个答案:

没有答案