-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_kmk.py
44 lines (37 loc) · 1.43 KB
/
send_kmk.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python3
# coding: utf-8
import serial.tools.list_ports
import time
def show_device():
# python -m serial.tools.list_ports
ports = list(serial.tools.list_ports.comports())
for k, p in enumerate(ports):
print(f"device {k}", p.description, p.hwid)
def send_kmk(kmk_config, data, enable_received=False, sleepTime=0.3):
with serial.Serial() as ser:
# ser.baudrate = 19200
ser.port = kmk_config["port"] # on windows example: COM8
ser.timeout = kmk_config["timeout"]
try:
ser.open()
except serial.serialutil.SerialException as e:
print(f'kmk Keyboard was not found. port: {kmk_config["port"]}')
return
send = b"keyboard.active_layers\n"
send = b"keyboard.tap_key(KC.Y)\n"
send = b"keyboard.tap_key(KC.TO(0))\n"
# send = b"keyboard.tap_key(KC.TO(1))\n"
send = bytearray(
f"{ ' '.join([str(i) for i in data])}\n", "utf-8"
) # 第一个是模式名字,后面参数,用空格,末尾带\n换行符
print(f'kmk_name: {kmk_config["name"]} kmk_port: {kmk_config["port"]}')
print(f"send_kmk: {send}")
ser.write(send)
if enable_received:
print(f"reading... wait {sleepTime} second")
time.sleep(sleepTime)
result = ser.readline()
print(f"result_kmk: {result}")
if __name__ == "__main__":
show_device()
# send_kmk()