-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecordingIOAsJson.py
More file actions
125 lines (108 loc) · 3.12 KB
/
RecordingIOAsJson.py
File metadata and controls
125 lines (108 loc) · 3.12 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
from pynput import mouse, keyboard
from datetime import datetime
import json
import threading
shift_symbol_map = {
'1': '!', '2': '@', '3': '#', '4': '$', '5': '%',
'6': '^', '7': '&', '8': '*', '9': '(', '0': ')',
'-': '_', '=': '+', '[': '{', ']': '}', ';': ':',
"'": '"', ',': '<', '.': '>', '/': '?', '\\': '|',
'`': '~'
}
keyboard_log = []
mouse_moves = []
mouse_clicks = []
start_time = datetime.now()
def now():
return (datetime.now() - start_time).total_seconds()
# 键盘监听
def on_press(key):
try:
keyboard_log.append({
'key': key.char,
'type': 'press',
'time': now()
})
except AttributeError:
keyboard_log.append({
'key': str(key),
'type': 'press',
'time': now()
})
def on_release(key):
if key == keyboard.Key.esc:
# 按 Esc 停止程序
return False
keyboard_log.append({
'key': str(key),
'type': 'release',
'time': now()
})
# 鼠标监听
def on_move(x, y):
mouse_moves.append({
'x': x,
'y': y,
'time': now()
})
def on_click(x, y, button, pressed):
mouse_clicks.append({
'x': x,
'y': y,
'button': str(button),
'pressed': pressed,
'time': now()
})
# 启动监听线程
def start_listeners():
kb_listener = keyboard.Listener(on_press=on_press, on_release=on_release)
ms_listener = mouse.Listener(on_move=on_move, on_click=on_click)
kb_listener.start()
ms_listener.start()
kb_listener.join()
ms_listener.stop()
def save_keyboard_txt(keyboard_log, output_path="keyboard_input.txt"):
shift_held = False
text_output = ""
for event in keyboard_log:
if event['type'] != 'press':
continue
k = event['key']
if k in ("Key.shift", "Key.shift_r", "Key.shift_l"):
shift_held = True
continue
elif k == "Key.enter":
text_output += "\n"
elif k == "Key.space":
text_output += " "
elif len(k) == 1 and ord(k) >= 32: # 只写入可见字符
if shift_held:
if k in shift_symbol_map:
text_output += shift_symbol_map[k]
else:
text_output += k.upper()
else:
text_output += k
elif k.startswith("Key."):
continue
else:
text_output += k
shift_held = False # 每个按键后重置 shift 状态
with open(output_path, "w", encoding="utf-8") as f:
f.write(text_output)
print(f"键盘文本已保存为 {output_path}")
# 主线程启动
if __name__ == "__main__":
print("监听中,按 ESC 退出...")
start_listeners()
# 保存 JSON 数据
data = {
'keyboard': keyboard_log,
'mouse_moves': mouse_moves,
'mouse_clicks': mouse_clicks
}
with open("input_log.json", "w", encoding="utf-8") as f:
json.dump(data, f, indent=2)
print("记录完成,已保存为 input_log.json")
# 保存 TXT 键盘文本
save_keyboard_txt(keyboard_log)