-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheatmap.ahk
More file actions
171 lines (143 loc) · 5.55 KB
/
heatmap.ahk
File metadata and controls
171 lines (143 loc) · 5.55 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#Persistent
#SingleInstance Force
SetWorkingDir %A_ScriptDir%
appName := "AHK Mouse Heatmap"
appDir := A_ScriptDir
iconFile := appDir . "\AHK-Mouse-Heatmap.ico"
logFile := appDir . "\ClickLog.txt"
heatmapFile := appDir . "\heatmap.png"
errorFile := appDir . "\heatmap_error.txt"
heatmapExe := appDir . "\generate_heatmap.exe"
heatmapScript := appDir . "\generate_heatmap.py"
if FileExist(iconFile)
Menu, Tray, Icon, %iconFile%
leftClicks := 0
rightClicks := 0
middleClicks := 0
; Create a CSV header on first run so the heatmap generator can parse the log reliably.
if !FileExist(logFile)
FileAppend, date,time,click,x,y`n, %logFile%
; Read existing log file to update click counts at script start.
ReadLogFileAndUpdateCounters()
; Add tray menu buttons.
Menu, Tray, Tip, %appName%
Menu, Tray, Add, Show, ShowGui
Menu, Tray, Add, Generate Heatmap, RunHeatmap
Menu, Tray, Add
Menu, Tray, Add, Exit, ExitScript
; Define GUI window with click counts and buttons.
Gui, MyGui:New, , %appName%
Gui, MyGui:Font, s10
Gui, MyGui:Add, Text, w260, Local click tracker and heatmap generator
Gui, MyGui:Add, Text, vLeftClicksText w260, Left clicks: %leftClicks%
Gui, MyGui:Add, Text, vRightClicksText w260, Right clicks: %rightClicks%
Gui, MyGui:Add, Text, vMiddleClicksText w260, Middle clicks: %middleClicks%
Gui, MyGui:Add, Button, gRunHeatmap w140, Generate Heatmap
Gui, MyGui:Add, Text, vStatusText w360, Ready.
; Show the GUI initially.
Gui, MyGui:Show, , %appName%
return
; Function to update GUI with current click counts.
UpdateGui:
global leftClicks, rightClicks, middleClicks
GuiControl, MyGui:, LeftClicksText, Left clicks: %leftClicks%
GuiControl, MyGui:, RightClicksText, Right clicks: %rightClicks%
GuiControl, MyGui:, MiddleClicksText, Middle clicks: %middleClicks%
return
; Function to read existing log file and update click counts.
ReadLogFileAndUpdateCounters() {
global leftClicks, rightClicks, middleClicks, logFile
if !FileExist(logFile)
return
FileRead, logContents, %logFile%
Loop, parse, logContents, `n, `r
{
line := A_LoopField
if (line = "" || line = "date,time,click,x,y")
continue
fields := StrSplit(line, ",")
if (fields.Length() < 5)
continue
clickType := fields[3]
if (clickType = "Left")
leftClicks++
else if (clickType = "Right")
rightClicks++
else if (clickType = "Middle")
middleClicks++
}
Gosub, UpdateGui
}
; Function to show GUI window when menu button is clicked.
ShowGui:
global appName
Gui, MyGui:Show, , %appName%
Gosub, UpdateGui
return
; Functions to increment click counts and log clicks to the file.
~LButton::IncrementAndLogClick("Left")
~RButton::IncrementAndLogClick("Right")
~MButton::IncrementAndLogClick("Middle")
IncrementAndLogClick(buttonType) {
global logFile, leftClicks, rightClicks, middleClicks
MouseGetPos, posX, posY
FormatTime, currentDate,, yyyy-MM-dd
FormatTime, currentTime,, HH:mm:ss
logEntry := currentDate . "," . currentTime . "," . buttonType . "," . posX . "," . posY . "`n"
FileAppend, %logEntry%, %logFile%
if (buttonType = "Left")
leftClicks++
else if (buttonType = "Right")
rightClicks++
else if (buttonType = "Middle")
middleClicks++
Gosub, UpdateGui
}
; Function to run heatmap generation script when menu button is clicked.
RunHeatmap:
global appDir, heatmapExe, heatmapScript, heatmapFile, errorFile, logFile
if !FileExist(logFile) {
MsgBox, 48, AHK Mouse Heatmap, No clicks have been logged yet. Click a few times, then generate the heatmap again.
return
}
if FileExist(heatmapFile)
FileDelete, %heatmapFile%
if FileExist(errorFile)
FileDelete, %errorFile%
GuiControl, MyGui:, StatusText, Generating heatmap...
if FileExist(heatmapExe) {
RunWait, "%heatmapExe%", %appDir%
generatorExitCode := ErrorLevel
} else if FileExist(heatmapScript) {
RunWait, python "%heatmapScript%", %appDir%
generatorExitCode := ErrorLevel
} else {
GuiControl, MyGui:, StatusText, Generator missing.
MsgBox, 16, AHK Mouse Heatmap, Could not find the heatmap generator.
return
}
if (generatorExitCode = "ERROR") {
GuiControl, MyGui:, StatusText, Could not launch generator.
MsgBox, 48, AHK Mouse Heatmap, Failed to launch the heatmap generator. If running from source, make sure Python is installed and available in PATH.
} else if FileExist(heatmapFile) {
GuiControl, MyGui:, StatusText, Heatmap generated.
MsgBox, 64, AHK Mouse Heatmap, Heatmap generated successfully.
} else if FileExist(errorFile) {
FileRead, generatorError, %errorFile%
GuiControl, MyGui:, StatusText, Heatmap generation failed.
MsgBox, 48, AHK Mouse Heatmap, %generatorError%
} else if (generatorExitCode != 0) {
GuiControl, MyGui:, StatusText, Heatmap generation failed.
MsgBox, 48, AHK Mouse Heatmap, The heatmap generator failed with exit code %generatorExitCode%.
} else {
GuiControl, MyGui:, StatusText, Heatmap not created.
MsgBox, 48, AHK Mouse Heatmap, The heatmap generator finished, but no heatmap was created.
}
return
; Function to exit the script when menu button is clicked.
ExitScript:
ExitApp
return
MyGuiGuiClose:
Gui, MyGui:Hide
return