-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyboardclient.py
44 lines (33 loc) · 1.05 KB
/
keyboardclient.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
import asyncio
from pynput import keyboard
from functools import partial
def on_press(writer, key):
char = None
try:
char = key.char
# print(char)
writer.write(char.encode())
except AttributeError:
if key == keyboard.Key.space:
writer.write(" ".encode())
if key == keyboard.Key.backspace:
writer.write("<".encode())
# print("special key {0} pressed".format(key))
def on_release(key):
# print("{0} released".format(key))
if key == keyboard.Key.esc:
# Stop listener
return False
async def tcp_echo_client(message):
reader, writer = await asyncio.open_connection("127.0.0.1", 8888)
# print(f"Send: {message!r}")
writer.write(message.encode())
await writer.drain()
with keyboard.Listener(
on_press=partial(on_press, writer), on_release=on_release, suppress=True
) as listener:
listener.join()
print("Close the connection")
writer.close()
await writer.wait_closed()
asyncio.run(tcp_echo_client("Hello World!"))