美文网首页
Python | socket 发送键盘状态

Python | socket 发送键盘状态

作者: T_K_233 | 来源:发表于2018-10-24 09:31 被阅读0次
    import win32api
    import time
    from struct import unpack
    
    keyList = ["\b"]
    for char in "ABCDEFGHIJKLMNOPQRSTUVWXYZ 123456789,.'APS$/\\":
        keyList.append(char)
    
    def key_check():
        keys = []
        for key in keyList:
            if win32api.GetAsyncKeyState(ord(key)):
                keys.append(key)
        return keys
    
    while True:
        # print(key_check())
        print(win32api.GetAsyncKeyState(65))
        time.sleep(0.33)
    

    然后在尝试通过串口发送所有的键盘按键

    import socket
    from struct import pack, unpack
    import time
    
    import win32api
    
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
    
    sock.bind(('0.0.0.0', 8080))
    sock.listen(1)
    
    conn, addr = sock.accept()
    print(conn)
    
    
    def get_key():
        keys = [0] * 255
        for i in range(255):
            keys[i] = bool(win32api.GetAsyncKeyState(i))
        bin_val = ''.join([str(int(i)) for i in keys])
        keys_a = int(bin_val[:64], 2)
        keys_b = int(bin_val[64:128], 2)
        keys_c = int(bin_val[128:192], 2)
        keys_d = int(bin_val[192:], 2)
        keee = list(bin(keys_a)[2:].zfill(64)+bin(keys_b)[2:].zfill(64)+bin(keys_c)[2:].zfill(64)+bin(keys_d)[2:].zfill(64))
        keee = [int(i) for i in keee]
        print(keee[65], keys[65])
        return keys_a, keys_b, keys_c, keys_d
    
    
    while True:
        keys = get_key()
        conn.send(pack('<BBQQQQ'+'B'*30, 61, 0, *keys, *[0]*30))
        time.sleep(0.033)
    

    但是编码和解码的速度不够........程序太卡了

    今天突然想到,其实不用把所有的按键状态都发送到板子上面......

    直接发送被按下的按键代码就可以......(因为一般人不会同时按下太多按键,键盘也不支持)

    相关文章

      网友评论

          本文标题:Python | socket 发送键盘状态

          本文链接:https://www.haomeiwen.com/subject/xehttqtx.html