import evdev,sys import pandas as pd from socket import * import time NULL_CHAR = chr(0) zero = NULL_CHAR*8 serHost = '192.168.0.111' serPort = 50009 data = pd.read_csv("./scan-code.csv") list = data.values.tolist() def read_line(code): code_hex = code.replace("0x","") #print("code_hex=",code_hex) for i in range(len(list)): lkc = str(list[i][2]) if lkc[-1] == "0": if code_hex == lkc.replace("0x00",""): return int(list[i][0].replace("0x07 0x00",""),16) else: if code_hex == lkc[lkc.rfind("0")+1:]: return int(list[i][0].replace("0x07 0x00",""),16) def send_report(report): sockobj.send(report.encode()) print(zero.encode()) sockobj = socket(AF_INET, SOCK_STREAM) sockobj.connect((serHost,serPort)) sockobj.send(b'hid open') data = sockobj.recv(1024) print(data) time.sleep(5) while True: for i in range(4,34): send_report(NULL_CHAR*2+chr(i)+NULL_CHAR*5) data = sockobj.recv(1024) print(data) send_report(NULL_CHAR*8) data = sockobj.recv(1024) i = i+1 if i == 35: i = 4 send_report(NULL_CHAR*2+chr(40)+NULL_CHAR*5) data = sockobj.recv(1024) print(data) send_report(NULL_CHAR*8) data = sockobj.recv(1024) time.sleep(0.1) device = evdev.InputDevice('/dev/input/event1') print(device) tmp = 0 tmp_code = 0 hang = 0 caps = 0 for event in device.read_loop(): if event.type == evdev.ecodes.EV_KEY : k = evdev.events.KeyEvent(event) #print(k) #完整事件 #print(event.code) ##键盘code --按键的hex code(如28---enter) hid_code = read_line(hex(event.code)) #print("hid_code=",hid_code)##完成scan code到hid code的转换 #print("hid_code_type=",type(hid_code)) #print(event.value) ##键盘状态 0,1,2 --0为release, 1为down , 2hold if event.value == 1: #print(NULL_CHAR*2+chr(event.code)+NULL_CHAR*5) if hid_code == 224 or hid_code == 225 or hid_code == 226 or hid_code == 227 or hid_code == 228 or hid_code == 229 or hid_code == 230 or hid_code == 231 or hid_code == 58: tmp_code = hid_code hang = 1 match hid_code: case 224: tmp = 1 case 225: tmp = 2 case 226: tmp = 4 case 227: tmp = 8 case 228: tmp = 16 case 229: tmp = 32 case 230: tmp = 64 case 231: tmp = 128 case 58: if caps == 1: caps = 0 else: caps = 1 hang = 0 tmp_code = 0 else: if tmp != 0: ##按住ctrl等然后按字母等 hang = 2 ##可以认定为没有单次敲击shift print("caps=",caps) if caps == 1 and (tmp_code == 225 or tmp_code == 229): send_report(NULL_CHAR*2+chr(hid_code)+NULL_CHAR*5) else: send_report(chr(tmp)+NULL_CHAR+chr(hid_code)+NULL_CHAR*5) else: if caps == 1 : send_report(chr(32)+NULL_CHAR+chr(hid_code)+NULL_CHAR*5) else: send_report(NULL_CHAR*2+chr(hid_code)+NULL_CHAR*5) data = sockobj.recv(1024) #print(data) if event.value == 2: #print(NULL_CHAR*2+chr(event.code)+NULL_CHAR*5) if hid_code == 224 or hid_code == 225 or hid_code == 226 or hid_code == 227 or hid_code == 228 or hid_code == 229 or hid_code == 230 or hid_code == 231 : tmp_code = hid_code match hid_code: case 224: tmp = 1 case 225: tmp = 2 case 226: tmp = 4 case 227: tmp = 8 case 228: tmp = 16 case 229: tmp = 32 case 230: tmp = 64 case 231: tmp = 128 else: if tmp != 0: hang = 2 if caps == 1 and (tmp_code == 225 or tmp_code == 229): send_report(NULL_CHAR*2+chr(hid_code)+NULL_CHAR*5) else: send_report(chr(tmp)+NULL_CHAR+chr(hid_code)+NULL_CHAR*5) else: if caps == 1 : send_report(chr(32)+NULL_CHAR+chr(hid_code)+NULL_CHAR*5) data = sockobj.recv(1024) #print(data) else: send_report(NULL_CHAR*2+chr(hid_code)+NULL_CHAR*5) data = sockobj.recv(1024) #print(data) if event.value == 0: if hid_code == tmp_code and hang == 1: send_report(chr(tmp)+NULL_CHAR*7) send_report(NULL_CHAR*8) tmp = 0 tmp_code = 0 hang = 0 elif hid_code == tmp_code: send_report(NULL_CHAR*8) tmp = 0 tmp_code = 0 hang = 0 else: send_report(NULL_CHAR*8) data = sockobj.recv(1024) #print(data) ''' This article provided the answer. Input reports (sent from keyboard to computer) have the following structure for a total of 8 bytes: 1 byte: modifier keys (Control, Shift, Alt, etc.), where each bit corresponds to a key 1 byte: unused/reserved for OEM 6 bytes: pressed key codes Further into the article, there is a list of the modifiers, each represented by a bit: Left Control: 224 (0x00e0)-------0x01 Left Shift: 225 (0x00e1)-------0x02 Left Alt: 226 (0x00e2)-------0x04 Left Meta (Windows key): 227 (0x00e3)-------0x08 Right Control: 228 (0x00e4)-------0x10 Right Shift: 229 (0x00e5)-------0x20 Right Alt: 230 (0x00e6)-------0x40 Right Meta (Windows key): 231 (0x00e7)-------0x80 For firing my screenshot app, I need CMD-SHIFT-5, so I need to set the 2nd and 4th bits on. 0b01010000 '''