import
[color=]csv
[color=]from
[color=]datetime
[color=]import
[color=]datetime
[color=]from
[color=]pynput
[color=]import
[color=]mouse
[color=]from
[color=]pygetwindow
[color=]import
[color=]getActiveWindow
[color=]from
[color=]pynput
[color=]import
[color=]mouse
# def on_click(x, y, button, pressed):
# if pressed:
# # 获取当前活动窗口
# active_window = getActiveWindow()
# if active_window:
# print(f"Mouse clicked. Active window: {active_window.title}")
# 初始化计数器
total_clicks
=
0
left_clicks
=
0
right_clicks
=
0
# 定义文件路径和CSV文件头
csv_file_path
=
[color=]'mouse_clicks_1127.csv'
csv_headers
=
[
[color=]'Click Type'
,
[color=]'X Coordinate'
,
[color=]'Y Coordinate'
,
[color=]'click_data'
,
[color=]'click_time'
,
[color=]'Total Clicks'
,
[color=]'Left Clicks'
,
[color=]'Right Clicks'
,
[color=]'Window'
]
# 创建一个函数来写入CSV文件(仅在程序开始时写入表头)
[color=]def
[color=]write_csv_header
(
file_path
,
headers
):
[color=]with
[color=]open
(
file_path
,
mode
=
[color=]'w'
,
newline
=
[color=]''
)
[color=]as
file
:
writer
=
[color=]csv
.
[color=]writer
(
file
)
writer
.
[color=]writerow
(
headers
)
# 定义一个函数来处理鼠标点击事件并写入CSV文件
[color=]def
[color=]on_click
(
x
,
y
,
button
,
pressed
):
[color=]global
total_clicks
,
left_clicks
,
right_clicks
[color=]if
pressed
:
click_data
=
[color=]datetime
.
[color=]now
().
[color=]strftime
(
[color=]'%Y-%m-
[color=]%d
[color=]'
)
click_time
=
[color=]datetime
.
[color=]now
().
[color=]strftime
(
[color=]'%H:%M:%S'
)
click_type
=
[color=]'Left'
[color=]if
button
==
[color=]mouse
.
[color=]Button
.
[color=]left
[color=]else
[color=]'Right'
active_window
=
[color=]getActiveWindow
()
[color=]print
(
[color=]f
[color=]"Mouse clicked. Active window:
[color=]{
active_window
.
title
[color=]}
[color=]"
)
# 更新点击次数
[color=]if
click_type
==
[color=]'Left'
:
left_clicks
+=
1
[color=]elif
click_type
==
[color=]'Right'
:
right_clicks
+=
1
total_clicks
+=
1
# 准备要写入CSV的数据行
row_data
=
[
click_type
,
x
,
1080
-
y
,
click_data
,
click_time
,
total_clicks
,
left_clicks
,
right_clicks
,
active_window
.
title
]
# 写入CSV文件
[color=]with
[color=]open
(
csv_file_path
,
mode
=
[color=]'a'
,
newline
=
[color=]''
)
[color=]as
file
:
writer
=
[color=]csv
.
[color=]writer
(
file
)
writer
.
[color=]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表头(如果文件不存在或为空)
[color=]try
:
# 尝试打开文件以检查其是否为空
[color=]with
[color=]open
(
csv_file_path
,
[color=]'r'
)
[color=]as
file
:
# 如果文件不为空,则不需要写入表头
first_line
=
file
.
[color=]readline
().
[color=]strip
()
[color=]if
first_line
:
# 文件不为空,表头已存在
[color=]pass
[color=]else
:
# 文件为空,写入表头
[color=]write_csv_header
(
csv_file_path
,
csv_headers
)
[color=]except
[color=]FileNotFoundError
:
# 文件不存在,写入表头
[color=]write_csv_header
(
csv_file_path
,
csv_headers
)
# 创建一个监听器,绑定鼠标点击事件处理函数
[color=]with
[color=]mouse
.
[color=]Listener
(
on_click
=
[color=]on_click
)
[color=]as
listener
:
[color=]print
(
[color=]"Start listening for mouse clicks. Press Ctrl+C to stop and save the data to a CSV file."
)
listener
.
[color=]join
()
# 这行代码会使程序一直运行,直到你手动停止它(例如,通过 Ctrl+C)