Home > Software design >  pynput presses are not being detected by some programs
pynput presses are not being detected by some programs

Time:08-10

I'm trying to create a remote using my phone for a game with sockets and pynput. Although some apps such as Chrome or Notepad detect and display the keys that have been pressed with the controller function of pynput (press and release), other ones (such as vba-m, the program I was interested in) just don't register any input. I've tried running the script with admin priviledges, but nothing has changed.

This is the code I'm using:

import socket,threading
from pynput.keyboard import Key, Controller
PORT=12345
SERVER="192.168.1.160"
ADDR=(SERVER, PORT)
FORMAT="utf-8"
TERMINATED="ADIOS"
server=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADDR)
conectados=[]
conectadosaddr=[]
keyboard = Controller()
def start(): #Función que se encarga de iniciar el servidor y escuchar nuevas conexiones
    server.listen()
    print("Servidor iniciado")
    while True:
        conn,addr=server.accept()
        conectados.append(conn)
        conectadosaddr.append(addr)
        thread=threading.Thread(target=handle_client, args=(conn, addr))
        thread.daemon=True
        thread.start()
        print(f"Conexiones activas: {len(conectados)}")
def handle_client(conn, addr): #Función que se encarga de manejar las conexiones
    print(f"Nueva conexion de {addr}")
    connected=True
    while connected:
        try:
            data=conn.recv(1024).decode(FORMAT)
            if data:
                print(f"Recibido: {data} de {addr}")
                if data==TERMINATED:
                    connected=False
                    print("Se ha cerrado la conexion pacíficamente de " str(addr))
                elif data in "WASDZX":
                    keyboard.press(data)
                    keyboard.release(data)
                elif data=="BK":
                    keyboard.press(Key.backspace)
                    keyboard.release(Key.backspace)
                elif data=="ST":
                    keyboard.press(Key.enter)
                    keyboard.release(Key.enter)
        except Exception as e:
            connected=False
            print("ERROR: ",e)
    print(f"{addr} se ha desconectado")
    conectados.remove(conn)
    conectadosaddr.remove(addr)
    conn.close()
if __name__ == '__main__':
    start()

CodePudding user response:

I managed to fix the issue using a different library, called "pydirectinput". It does the same thing pynput does, but its presses are detected by vba-m and many other videogames

  • Related