# 写入CSV文件
with open(csv_file_path, mode='a', newline='') as file:
writer = csv.writer(file)
writer.writerow(row_data)
# 打印点击信息到控制台(可选)
print(f'{click_type} click at ({x}, {y}) at {click_data} {click_time} (Total clicks: {total_clicks}, Left clicks: {left_clicks}, Right clicks: {right_clicks})')
# 在程序开始时写入CSV表头(如果文件不存在或为空)
try:
# 尝试打开文件以检查其是否为空
with open(csv_file_path, 'r') as file:
# 如果文件不为空,则不需要写入表头
first_line = file.readline().strip()
if first_line:
# 文件不为空,表头已存在
pass
else:
# 文件为空,写入表头
write_csv_header(csv_file_path, csv_headers)
except FileNotFoundError:
# 文件不存在,写入表头
write_csv_header(csv_file_path, csv_headers)
# 创建一个监听器,绑定鼠标点击事件处理函数
with mouse.Listener(on_click=on_click) as listener:
print("Start listening for mouse clicks. Press Ctrl+C to stop and save the data to a CSV file.")
listener.join() # 这行代码会使程序一直运行,直到你手动停止它(例如,通过 Ctrl+C)