-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHotspotHelper.ahk
1405 lines (1361 loc) · 51.9 KB
/
HotspotHelper.ahk
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#Requires AutoHotkey v2.0
#MaxThreadsPerHotkey 1
#SingleInstance Force
#Warn All
SendMode "Input"
SetTitleMatchMode 2
SetWorkingDir A_InitialWorkingDir
CoordMode "Caret", "Client"
CoordMode "Menu", "Client"
CoordMode "Mouse", "Client"
CoordMode "Pixel", "Client"
CoordMode "ToolTip", "Client"
#Include <ScreenArea2File>
#Include <OCR>
AppName := "HotspotHelper"
CurrentHotspot := 0
DialogOpen := 0
Hotspots := Array()
KeyboardMode := 0
LastImage := ""
MouseXPosition := ""
MouseYPosition := ""
UnlabelledHotspotLabel := "unlabelled Hotspot"
Try
JAWS := ComObject("FreedomSci.JawsApi")
Catch
JAWS := False
Try
SAPI := ComObject("SAPI.SpVoice")
Catch
SAPI := False
A_IconTip := AppName
A_TrayMenu.Delete()
A_TrayMenu.Add("&About...", About)
A_TrayMenu.Add("&Quit...", Quit)
#+A::About()
#+C::CopyControlInfoToClipboard()
#+Del::DeleteAllHotspots()
#+Down::MoveMouseDown()
#+Enter::AddHotspot()
#+F::FocusControl()
#+G::GenerateHotspotsFromOCR()
#+H::CopyHotspotsToClipboard()
#+I::SearchForImage()
#+K::ToggleKeyboardMode()
#+L::CopyControlListToClipboard()
#+Left::MoveMouseLeft()
#+M::RouteMouseToFocusedControl()
#+N::CopyPixelColorToClipboard()
#+O::PerformOCR()
#+P::SearchForColor()
#+PrintScreen::ExtractImage()
#+Q::Quit()
#+R::SearchForImage(LastImage)
#+Right::MoveMouseRight()
#+U::ReportPixelColor()
#+Up::MoveMouseUp()
#+V::ViewClipboard()
#+W::CopyWindowInfoToClipboard()
#+X::SetMouseXPosition()
#+Y::SetMouseYPosition()
#+Z::ReportMousePosition()
+Tab::SelectPreviousHotspot()
Ctrl::StopSpeech()
Del::DeleteHotspot()
Enter::ClickHotspot()
F2::RenameHotspot()
Tab::SelectNextHotspot()
SetTimer ManageHotkeys, 100
About()
About(*) {
Global AppName, DialogOpen
Static FirstShow := True
If DialogOpen = 0 {
DialogOpen := 1
AboutBox := Gui(, "About " . AppName)
AboutBox.AddEdit("ReadOnly -WantReturn", "Use this tool to determine hotspot mouse coordinates, obtain information about the active window and its controls and copy the retrieved info to clipboard.`nEnable keyboard mode whenever you want to click, delete or rename previously added individual hotspots.`n`nKeyboard Shortcuts`n`nHotspot Shortcuts:`nShift+Win+Enter - Add hotspot`nShift+Win+Del - Delete all hotspots`nShift+Win+H - Copy hotspots to clipboard`nKeyboard Mode Shortcuts:`nShift+Win+K - Toggle keyboard mode on/off`nTab - Select next hotspot`nShift+Tab - Select previous hotspot`nEnter - Click current hotspot`nDel - Delete current hotspot`nF2 - Rename current hotspot`n`nWindow & Control Shortcuts:`nShift+Win+W - Copy info about the active window to clipboard`nShift+Win+C - Copy info about the currently focused control to clipboard`nShift+Win+L - Copy control list to clipboard`nShift+Win+F - Focus control`n`nMouse Shortcuts:`nShift+Win+M - Route the mouse to the position of the currently focused control`nShift+Win+U - Report the pixel color under the mouse cursor`nShift+Win+N - Copy the pixel color under the mouse cursor to clipboard`nShift+Win+X - Set mouse X position`nShift+Win+Y - Set mouse Y position`nShift+Win+Z - Report mouse position`nShift+Win+Left - Move mouse leftf`nShift+Win+Right - Move mouse right`nShift+Win+Up - Move mouse up`nShift+Win+Down - Move mouse down`n`nMiscellaneous Shortcuts:`nShift+Win+Print Screen - Extract a region of the active window as an image`nShift+Win+O - OCR the active window`nShift+Win+G - Generate hotspots from OCR`nShift+Win+P - Search for color`nShift+Win+I - Search for image`nShift+Win+R - Repeat search using last image`nShift+Win+V - Open Clipboard Viewer`nShift+Win+A - About the app`nShift+Win+Q - Quit the app`nCtrl - Stop speech")
AboutBox.AddButton("Default", "OK").OnEvent("Click", CloseAboutBox)
AboutBox.OnEvent("Close", CloseAboutBox)
AboutBox.OnEvent("Escape", CloseAboutBox)
AboutBox.Show()
CloseAboutBox(*) {
AboutBox.Destroy()
DialogOpen := 0
If FirstShow = True {
FirstShow := False
Sleep 500
Speak(AppName . " ready")
}
}
}
}
AddHotspot() {
Global AppName, CurrentHotspot, DialogOpen, Hotspots, MouseXposition, MouseYPosition, UnlabelledHotspotLabel
If DialogOpen = 0 {
DialogOpen := 1
If CurrentHotspot < Hotspots.Length
CurrentHotspot++
Else
CurrentHotspot := Hotspots.Length + 1
MouseGetPos &mouseXPosition, &mouseYPosition
NameDialog := InputBox("Enter A Name For This Hotspot.", AppName)
If NameDialog.Result == "OK" {
If Not NameDialog.Value = ""
Label := NameDialog.Value
Else
Label := UnlabelledHotspotLabel
Hotspots.InsertAt(CurrentHotspot, Map())
Hotspots[CurrentHotspot]["Label"] := Label
Hotspots[CurrentHotspot]["XCoordinate"] := MouseXPosition
Hotspots[CurrentHotspot]["YCoordinate"] := MouseYPosition
Sleep 25
Speak(Hotspots[CurrentHotspot]["Label"])
}
DialogOpen := 0
}
}
ClickHotspot() {
Global CurrentHotspot, DialogOpen, Hotspots
Global DialogOpen
If DialogOpen = 0
If Hotspots.Length > 0 And CurrentHotspot > 0 And CurrentHotspot <= Hotspots.Length {
Click Hotspots[CurrentHotspot]["XCoordinate"], Hotspots[CurrentHotspot]["YCoordinate"]
Sleep 25
Speak("Hotspot clicked")
}
Else {
Sleep 25
Speak("No hotspot selected")
}
}
ClientCoordToScreenCoord(X, Y) {
CoordMode "Mouse", "Client"
Try {
WinWaitActive("A")
MouseMove X, Y
CoordMode "Mouse", "Screen"
MouseGetPos &mouseXPos, &mouseYPos
}
Catch {
MouseXPos := False
MouseYPos := False
}
CoordMode "Mouse", "Client"
Return {X: MouseXPos, Y: MouseYPos}
}
CopyControlInfoToClipboard() {
Global AppName, DialogOpen
If DialogOpen = 0 {
Try {
WinWaitActive("A")
Try
ControlClass := ControlGetClassNN(ControlGetFocus("A"))
Catch
ControlClass := ""
Try {
ControlGetPos &ControlX, &ControlY, &ControlW, &ControlH, ControlClass, "A"
ControlDimensions := ControlW . " × " . ControlH
ControlPos := "X " . ControlX . ", Y " . ControlY
}
Catch {
ControlDimensions := ""
ControlPos := ""
}
ControlHwnd := ControlGetHwnd(ControlClass, "A")
Try {
WinGetPos &WinX, &WinY, &WinW, &WinH, ControlHwnd
WinDimensions := WinW . " × " . WinH
WinPos := "X " . WinX . ", Y " . WinY
}
Catch {
WinDimensions := ""
WinPos := ""
}
Try {
WinGetClientPos &ClientX, &ClientY, &ClientW, &ClientH, ControlHwnd
ClientDimensions := ClientW . " × " . ClientH
ClientPos := "X " . ClientX . ", Y " . ClientY
}
Catch {
ClientDimensions := ""
ClientPos := ""
}
}
Catch {
Speak("Focused control not found")
Return
}
DialogOpen := 1
ConfirmationDialog := MsgBox("Copy info About the currently focused control to clipboard?", AppName, 4)
If ConfirmationDialog == "Yes" {
Sleep 1000
A_Clipboard := "Control class: " . ControlClass . "`nControl Position: " . ControlPos . "`nControl Dimensions: " . ControlDimensions . "`n`nWhen Treated As A Window:`nWindow Position: " . WinPos . "`nWindow Dimensions: " . WinDimensions . "`nClient Area Position: " . ClientPos . "`nClient Area Dimensions: " . ClientDimensions
Speak("Control info copied to clipboard")
}
DialogOpen := 0
}
}
CopyControlListToClipboard() {
Global AppName, DialogOpen
If DialogOpen = 0 {
Try {
WinWaitActive("A")
Controls := WinGetControls("A")
}
Catch {
Speak("No controls found")
Return
}
DialogOpen := 1
ConfirmationDialog := MsgBox("Copy control list to clipboard?", AppName, 4)
If ConfirmationDialog == "Yes" {
Sleep 1000
ClipboardData := "Control class`tX coordinate`tY coordinate`tWidth`tHeight`r`n"
For Control In controls {
ControlClass := ControlGetClassNN(Control)
Try {
ControlGetPos &ControlX, &ControlY, &ControlW, &ControlH, ControlClass, "A"
}
Catch {
ControlX := "-"
ControlY := "-"
ControlW := "-"
ControlH := "-"
}
ClipboardData .= ControlClass . "`t" . ControlX . "`t" . ControlY . "`t" . ControlW . "`t" . ControlH . "`r`n"
}
ClipboardData := RTrim(ClipboardData, "`r`n")
A_Clipboard := ClipboardData
If Controls.Length = 1
Speak("1 control copied to clipboard")
Else
Speak(Controls.Length . " controls copied to clipboard")
}
DialogOpen := 0
}
}
CopyHotspotsToClipboard() {
Global AppName, DialogOpen, Hotspots
If DialogOpen = 0 {
Try {
WinWaitActive("A")
ControlClass := ControlGetClassNN(ControlGetFocus("A"))
ControlGetPos &ControlX, &ControlY,,, ControlClass, "A"
}
Catch {
ControlClass := False
ControlX := 0
ControlY := 0
}
If Hotspots.Length = 0 {
Speak("No hotspots defined")
Return
}
DialogOpen := 1
ConfirmationDialog := MsgBox("Copy hotspots to clipboard?", AppName, 4)
If ConfirmationDialog == "Yes" {
ClipboardData := ""
ConfirmationDialog := MsgBox("Compensate for the position of the currently focused control?", AppName, 4)
If ConfirmationDialog == "Yes" {
Sleep 1000
If ControlClass = False {
Speak("Focused control not found")
}
Else {
ClipboardData .= "Compensating for X " . ControlX . ", Y " . ControlY . "`r`n"
For Value In Hotspots {
Label := Value["Label"]
MouseXCoordinate := Value["XCoordinate"] - ControlX
MouseYCoordinate := Value["YCoordinate"] - ControlY
ClipboardData .= "`"" . Label . "`", " . MouseXCoordinate . ", " . MouseYCoordinate . "`r`n"
}
}
}
Else {
Sleep 1000
For Value In Hotspots {
Label := Value["Label"]
MouseXCoordinate := Value["XCoordinate"]
MouseYCoordinate := Value["YCoordinate"]
ClipboardData .= "`"" . Label . "`", " . MouseXCoordinate . ", " . MouseYCoordinate . "`r`n"
}
}
ClipboardData := RTrim(ClipboardData, "`r`n")
A_Clipboard := ClipboardData
If Hotspots.Length = 1
Speak("1 hotspot copied to clipboard")
Else
Speak(Hotspots.Length . " hotspots copied to clipboard")
}
}
DialogOpen := 0
}
CopyPixelColorToClipboard() {
Global AppName, DialogOpen, MouseXPosition, MouseYPosition
If DialogOpen = 0 {
DialogOpen := 1
ConfirmationDialog := MsgBox("Copy the color of the pixel currently under the mouse to clipboard?", AppName, 4)
If ConfirmationDialog == "Yes" {
Sleep 1000
MouseGetPos &mouseXPosition, &mouseYPosition
A_Clipboard := PixelGetColor(MouseXPosition, MouseYPosition, "Slow")
Speak("Pixel color copied to clipboard")
}
DialogOpen := 0
}
}
CopyWindowInfoToClipboard() {
Global AppName, DialogOpen
If DialogOpen = 0 {
Try {
WinWaitActive("A")
Try
ProcessName := WinGetProcessName("A")
Catch
ProcessName := ""
Try
ProcessPath := WinGetProcessPath("A")
Catch
ProcessPath := ""
Try
WindowClass := WinGetClass("A")
Catch
WindowClass := ""
Try
WindowID := WinGetID("A")
Catch
WindowID := ""
Try
WindowTitle := WinGetTitle("A")
Catch
WindowTitle := ""
Try {
WinGetPos &WinX, &WinY, &WinW, &WinH, "A"
WinDimensions := WinW . " × " . WinH
WinPos := "X " . WinX . ", Y " . WinY
}
Catch {
WinDimensions := ""
WinPos := ""
}
Try {
WinGetClientPos &ClientX, &ClientY, &ClientW, &ClientH, "A"
ClientDimensions := ClientW . " × " . ClientH
ClientPos := "X " . ClientX . ", Y " . ClientY
}
Catch {
ClientDimensions := ""
ClientPos := ""
}
}
Catch {
Speak("Active window not found")
Return
}
DialogOpen := 1
ConfirmationDialog := MsgBox("Copy info about the active window to clipboard?", AppName, 4)
If ConfirmationDialog == "Yes" {
Sleep 1000
A_Clipboard := "Process Name: " . ProcessName . "`nProcess Path: " . ProcessPath . "`nWindow Class: " . WindowClass . "`nWindow ID: " . WindowID . "`nWindow Title: " . WindowTitle . "`nWindow Position: " . WinPos . "`nWindow Dimensions: " . WinDimensions . "`nClient Area Position: " . ClientPos . "`nClient Area Dimensions: " . ClientDimensions
Speak("Window info copied to clipboard")
}
DialogOpen := 0
}
}
DeleteAllHotspots() {
Global AppName, CurrentHotspot, DialogOpen, Hotspots
If DialogOpen = 0 {
If Hotspots.Length = 0 {
Speak("No hotspots defined")
Return
}
DialogOpen := 1
ConfirmationDialog := MsgBox("Delete all hotspots?", AppName, 4)
If ConfirmationDialog == "Yes" {
Sleep 1000
HotspotCount := Hotspots.Length
CurrentHotspot := 0
Hotspots := Array()
If HotspotCount = 1
Speak("1 hotspot deleted")
Else
Speak(HotspotCount . " hotspots deleted")
}
DialogOpen := 0
}
}
DeleteHotspot() {
Global CurrentHotspot, DialogOpen, Hotspots
Global DialogOpen
If DialogOpen = 0
If Hotspots.Length > 0 And CurrentHotspot > 0 And CurrentHotspot <= Hotspots.Length {
Hotspots.RemoveAt(CurrentHotspot)
If CurrentHotspot > 1
CurrentHotspot--
Sleep 25
Speak("Hotspot deleted")
}
Else {
Sleep 25
Speak("No hotspot selected")
}
}
ExtractImage(*) {
Global AppName, DialogOpen
Static X1Coord := "", Y1Coord := "", X2Coord := "", Y2Coord := ""
Try {
WindowID := WinGetID("A")
WindowTitle := WinGetTitle("A")
WinGetPos ,, &XSize, &YSize, "A"
}
Catch {
Speak("Could not obtain required information")
Return
}
If DialogOpen = 0 {
DialogOpen := 1
CoordinateBox := Gui(, AppName . " Image Extractor {`"" . WindowTitle . "`"}")
CoordinateBox.AddText(, "X1 coordinate (between 0 and " . XSize . "):")
CoordinateBox.AddEdit("vX1Coord Number YS", X1Coord).OnEvent("Change", ProcessInput)
CoordinateBox.AddText("YS", "Y1 coordinate (between 0 and " . YSize . "):")
CoordinateBox.AddEdit("vY1Coord Number YS", Y1Coord).OnEvent("Change", ProcessInput)
CoordinateBox.AddButton("YS", "Select hotspot...").OnEvent("Click", ShowHotspotMenu1)
CoordinateBox.AddText("Section XS", "X2 coordinate (between 0 and " . XSize . "):")
CoordinateBox.AddEdit("vX2Coord Number YS", X2Coord).OnEvent("Change", ProcessInput)
CoordinateBox.AddText("YS", "Y2 coordinate (between 0 and " . YSize . "):")
CoordinateBox.AddEdit("vY2Coord Number YS", Y2Coord).OnEvent("Change", ProcessInput)
CoordinateBox.AddButton("YS", "Select hotspot...").OnEvent("Click", ShowHotspotMenu2)
CoordinateBox.AddButton("Disabled Section XS", "OK").OnEvent("Click", SaveImage)
CoordinateBox.AddButton("Default YS", "Cancel").OnEvent("Click", CloseCoordinateBox)
CoordinateStatus := CoordinateBox.Add("StatusBar")
CoordinateBox.OnEvent("Close", CloseCoordinateBox)
CoordinateBox.OnEvent("Escape", CloseCoordinateBox)
CoordinateBox.Show()
HotspotMenuHandler(MenuItemLabel, MenuItemNumber, HotspotMenu) {
Global Hotspots
CoordinateBox["X" . HotspotMenu.Number . "Coord"].Value := Hotspots[MenuItemNumber - 1]["XCoordinate"]
CoordinateBox["Y" . HotspotMenu.Number . "Coord"].Value := Hotspots[MenuItemNumber - 1]["YCoordinate"]
ProcessInput()
}
ProcessInput()
CloseCoordinateBox(*) {
CoordinateBox.Destroy()
DialogOpen := 0
}
ProcessInput(*) {
ControlValues := CoordinateBox.Submit(False)
X1 := ControlValues.X1Coord
Y1 := ControlValues.Y1Coord
X2 := ControlValues.X2Coord
Y2 := ControlValues.Y2Coord
If Not X1 = "" And Not Y1 = "" And Not X2 = "" And Not Y2 = "" And X1 >= 0 And X1 < X2 And X2 <= XSize And Y1 >= 0 And Y1 < Y2 And Y2 <= YSize {
CoordinateBox["OK"].Opt("+Default -Disabled")
CoordinateBox["Cancel"].Opt("-Default")
CoordinateStatus.SetText("`tImage dimensions " . X2 - X1 . " × " . Y2 - Y1)
}
Else {
CoordinateBox["OK"].Opt("-Default +Disabled")
CoordinateBox["Cancel"].Opt("+Default")
CoordinateStatus.SetText("`tImage dimensions unknown")
}
}
SaveImage(*) {
ControlValues := CoordinateBox.Submit(True)
ControlValues := CoordinateBox.Submit(False)
X1Coord := ControlValues.X1Coord
Y1Coord := ControlValues.Y1Coord
X2Coord := ControlValues.X2Coord
Y2Coord := ControlValues.Y2Coord
X1 := ControlValues.X1Coord
Y1 := ControlValues.Y1Coord
X2 := ControlValues.X2Coord
Y2 := ControlValues.Y2Coord
If Not WinExist("ahk_id" . WindowID) {
MsgBox "Target window not found.", AppName
CloseCoordinateBox()
}
Else {
WinActivate("ahk_id" . WindowID)
WinWaitActive("ahk_id" . WindowID)
Coord := ClientCoordToScreenCoord(X1, Y1)
X1 := Coord.X
Y1 := Coord.Y
Coord := ClientCoordToScreenCoord(X2, Y2)
X2 := Coord.X
Y2 := Coord.Y
If X1 Is Number And Y1 Is Number And X2 Is Number And Y2 Is Number {
W := X2 - X1
H := Y2 - Y1
ImageFile := FileSelect("S18", "Image", "Save Image", "PNG Files (*.png)")
If ImageFile {
SplitPath ImageFile, &FileName, &Directory, &Extension
If Extension = "" {
FileName := FileName . ".png"
ImageFile .= ".png"
}
Sleep 500
ScreenArea2File(Directory, FileName, {X: X1, Y: Y1, W: W, H: H})
If Not FileExist(ImageFile) Or InStr(FileExist(ImageFile), "D") {
MsgBox "An error occurred while saving file.`nPlease try again.", AppName
CoordinateBox.Show()
}
Else {
MsgBox "File saved successfully.", AppName
CloseCoordinateBox()
}
}
Else {
CloseCoordinateBox()
}
}
Else {
MsgBox "An error occurred.`nPlease try again.", AppName
CoordinateBox.Show()
}
}
}
}
ShowHotspotMenu(MenuNumber) {
Global hotspots
HotspotMenu := Menu()
HotspotMenu.Number := MenuNumber
If Hotspots.Length = 0 {
HotspotMenu.Add("No hotspots", HotspotMenuHandler)
HotspotMenu.Disable("No hotspots")
}
Else {
HotspotMenu.Add("Select hotspot", HotspotMenuHandler)
HotspotMenu.Disable("Select hotspot")
For HotspotNumber, Hotspot In Hotspots
HotspotMenu.Add(HotspotNumber . ". " . Hotspot["Label"], HotspotMenuHandler)
}
HotspotMenu.Show()
}
ShowHotspotMenu1(*) {
ShowHotspotMenu(1)
}
ShowHotspotMenu2(*) {
ShowHotspotMenu(2)
}
}
FocusControl() {
Global AppName, DialogOpen
If DialogOpen = 0 {
Try {
WinWaitActive("A")
Controls := WinGetControls("A")
ControlClass := ControlGetClassNN(ControlGetFocus("A"))
}
Catch {
Controls := Array()
ControlClass := False
}
If Controls.Length = 0 {
Speak("No controls found")
Return
}
DialogOpen := 1
ControlMenu := Menu()
For Control In controls {
ControlMenu.Add(Control, ControlMenuHandler)
If Control = ControlClass
ControlMenu.Check(control)
}
ControlMenu.Show()
}
DialogOpen := 0
ControlMenuHandler(ControlName, ControlNumber, ControlMenu) {
Try {
WinWaitActive("A")
ControlFocus ControlName, "A"
If ControlGetClassNN(ControlGetFocus("A")) = ControlName {
Speak(ControlName . " focused")
}
}
Catch {
Speak("Control not found")
}
}
}
GenerateHotspotsFromOCR() {
Global AppName, DialogOpen, Hotspots
If DialogOpen = 0 {
DialogOpen := 1
Try {
WinWaitActive("A")
ConfirmationDialog := MsgBox("Generate hotspots from OCR?", AppName, 4)
If ConfirmationDialog == "Yes" {
AvailableLanguages := OCR.GetAvailableLanguages()
FirstAvailableLanguage := False
FirstOCRLanguage := False
PreferredLanguage := False
PreferredOCRLanguage := ""
Loop Parse, AvailableLanguages, "`n" {
If A_Index = 1 And Not A_LoopField = "" {
FirstAvailableLanguage := True
FirstOCRLanguage := A_LoopField
}
If SubStr(A_LoopField, 1, 3) = "en-" {
PreferredLanguage := True
PreferredOCRLanguage := A_LoopField
Break
}
}
If FirstAvailableLanguage = False And PreferredLanguage = False {
Sleep 25
Speak("No OCR languages installed!")
}
Else {
If PreferredLanguage = False
OCRLanguage := FirstOCRLanguage
Else
OCRLanguage := PreferredOCRLanguage
OCRResult := OCR.FromWindow("A", OCRLanguage,, 1)
For OCRLine In OCRResult.Lines {
Hotspots.Push(Map("Label", "`"" . OCRLine.Text . "`" start", "XCoordinate", OCR.WordsBoundingRect(OCRLine.Words*).X, "YCoordinate", OCR.WordsBoundingRect(OCRLine.Words*).Y))
Hotspots.Push(Map("Label", "`"" . OCRLine.Text . "`" end", "XCoordinate", OCR.WordsBoundingRect(OCRLine.Words*).X + OCR.WordsBoundingRect(OCRLine.Words*).W, "YCoordinate", OCR.WordsBoundingRect(OCRLine.Words*).Y + OCR.WordsBoundingRect(OCRLine.Words*).H))
}
Speak(OCRResult.Lines.Length * 2 . " hotspots generated")
}
}
}
DialogOpen := 0
}
}
ManageHotkeys() {
Global DialogOpen, KeyboardMode
If DialogOpen = 1 Or WinActive("ahk_exe Explorer.Exe") Or WinActive("ahk_class Shell_TrayWnd" Or WinExist("ahk_class #32768") ) {
Hotkey "#+A", "On"
Hotkey "#+C", "On"
Hotkey "#+Del", "Off"
Hotkey "#+Down", "On"
Hotkey "#+Enter", "Off"
Hotkey "#+F", "On"
Hotkey "#+G", "Off"
Hotkey "#+H", "On"
Hotkey "#+I", "On"
Hotkey "#+K", "Off"
Hotkey "#+L", "On"
Hotkey "#+Left", "On"
Hotkey "#+M", "On"
Hotkey "#+N", "On"
Hotkey "#+O", "On"
Hotkey "#+P", "On"
Hotkey "#+PrintScreen", "On"
Hotkey "#+Q", "On"
Hotkey "#+R", "On"
Hotkey "#+Right", "On"
Hotkey "#+U", "On"
Hotkey "#+Up", "On"
Hotkey "#+V", "On"
Hotkey "#+W", "On"
Hotkey "#+Z", "On"
Hotkey "+Tab", "Off"
Hotkey "Ctrl", "Off"
Hotkey "Del", "Off"
Hotkey "Enter", "Off"
Hotkey "F2", "Off"
Hotkey "Tab", "Off"
}
Else If KeyboardMode = 1 {
Hotkey "#+A", "On"
Hotkey "#+C", "On"
Hotkey "#+Del", "On"
Hotkey "#+Down", "On"
Hotkey "#+Enter", "On"
Hotkey "#+F", "On"
Hotkey "#+G", "On"
Hotkey "#+H", "On"
Hotkey "#+I", "On"
Hotkey "#+K", "On"
Hotkey "#+L", "On"
Hotkey "#+Left", "On"
Hotkey "#+M", "On"
Hotkey "#+N", "On"
Hotkey "#+O", "On"
Hotkey "#+P", "On"
Hotkey "#+PrintScreen", "On"
Hotkey "#+Q", "On"
Hotkey "#+R", "On"
Hotkey "#+Right", "On"
Hotkey "#+U", "On"
Hotkey "#+Up", "On"
Hotkey "#+V", "On"
Hotkey "#+W", "On"
Hotkey "#+Z", "On"
Hotkey "+Tab", "On"
Hotkey "Ctrl", "On"
Hotkey "Del", "On"
Hotkey "Enter", "On"
Hotkey "F2", "On"
Hotkey "Tab", "On"
}
Else {
Hotkey "#+A", "On"
Hotkey "#+C", "On"
Hotkey "#+Del", "On"
Hotkey "#+Down", "On"
Hotkey "#+Enter", "On"
Hotkey "#+F", "On"
Hotkey "#+G", "On"
Hotkey "#+H", "On"
Hotkey "#+I", "On"
Hotkey "#+K", "On"
Hotkey "#+L", "On"
Hotkey "#+Left", "On"
Hotkey "#+M", "On"
Hotkey "#+N", "On"
Hotkey "#+O", "On"
Hotkey "#+P", "On"
Hotkey "#+PrintScreen", "On"
Hotkey "#+Q", "On"
Hotkey "#+R", "On"
Hotkey "#+Right", "On"
Hotkey "#+U", "On"
Hotkey "#+Up", "On"
Hotkey "#+V", "On"
Hotkey "#+W", "On"
Hotkey "#+Z", "On"
Hotkey "+Tab", "Off"
Hotkey "Ctrl", "On"
Hotkey "Del", "Off"
Hotkey "Enter", "Off"
Hotkey "F2", "Off"
Hotkey "Tab", "Off"
}
}
MoveMouseDown() {
Global DialogOpen
If DialogOpen = 0 {
Try {
WinGetClientPos ,,, &YSize, "A"
}
Catch {
Speak("Could not identify window size")
Return
}
DialogOpen := 1
MouseGetPos &MouseXCoordinate, &MouseYCoordinate
If MouseYCoordinate < 0
TargetCoordinate := 0
Else If MouseYCoordinate > YSize
TargetCoordinate := YSize
Else
TargetCoordinate := MouseYCoordinate + 1
If TargetCoordinate > YSize
TargetCoordinate := YSize
MouseYCoordinate := TargetCoordinate
MouseMove MouseXCoordinate, MouseYCoordinate
Speak(MouseYCoordinate)
DialogOpen := 0
}
}
MoveMouseLeft() {
Global DialogOpen
If DialogOpen = 0 {
Try {
WinGetClientPos ,, &XSize,, "A"
}
Catch {
Speak("Could not identify window size")
Return
}
DialogOpen := 1
MouseGetPos &MouseXCoordinate, &MouseYCoordinate
If MouseXCoordinate < 0
TargetCoordinate := 0
Else If MouseXCoordinate > XSize
TargetCoordinate := XSize
Else
TargetCoordinate := MouseXCoordinate - 1
If TargetCoordinate < 0
TargetCoordinate := 0
MouseXCoordinate := TargetCoordinate
MouseMove MouseXCoordinate, MouseYCoordinate
Speak(MouseXCoordinate)
DialogOpen := 0
}
}
MoveMouseRight() {
Global DialogOpen
If DialogOpen = 0 {
Try {
WinGetClientPos ,, &XSize,, "A"
}
Catch {
Speak("Could not identify window size")
Return
}
DialogOpen := 1
MouseGetPos &MouseXCoordinate, &MouseYCoordinate
If MouseXCoordinate < 0
TargetCoordinate := 0
Else If MouseXCoordinate > XSize
TargetCoordinate := XSize
Else
TargetCoordinate := MouseXCoordinate + 1
If TargetCoordinate > XSize
TargetCoordinate := XSize
MouseXCoordinate := TargetCoordinate
MouseMove MouseXCoordinate, MouseYCoordinate
Speak(MouseXCoordinate)
DialogOpen := 0
}
}
MoveMouseUp() {
Global DialogOpen
If DialogOpen = 0 {
Try {
WinGetClientPos ,,, &YSize, "A"
}
Catch {
Speak("Could not identify window size")
Return
}
DialogOpen := 1
MouseGetPos &MouseXCoordinate, &MouseYCoordinate
If MouseYCoordinate < 0
TargetCoordinate := 0
Else If MouseYCoordinate > YSize
TargetCoordinate := YSize
Else
TargetCoordinate := MouseYCoordinate - 1
If TargetCoordinate < 0
TargetCoordinate := 0
MouseYCoordinate := TargetCoordinate
MouseMove MouseXCoordinate, MouseYCoordinate
Speak(MouseYCoordinate)
DialogOpen := 0
}
}
PerformOCR() {
Global AppName, DialogOpen
If DialogOpen = 0 {
DialogOpen := 1
Try {
WinWaitActive("A")
ControlClass := ControlGetClassNN(ControlGetFocus("A"))
ControlGetPos &ControlX, &ControlY,,, ControlClass, "A"
}
Catch {
ControlClass := False
ControlX := 0
ControlY := 0
}
ConfirmationDialog := MsgBox("OCR the active Window and copy the results to clipboard?", AppName, 4)
If ConfirmationDialog == "Yes" {
AvailableLanguages := OCR.GetAvailableLanguages()
FirstAvailableLanguage := False
FirstOCRLanguage := False
PreferredLanguage := False
PreferredOCRLanguage := ""
Loop Parse, AvailableLanguages, "`n" {
If A_Index = 1 And Not A_LoopField = "" {
FirstAvailableLanguage := True
FirstOCRLanguage := A_LoopField
}
If SubStr(A_LoopField, 1, 3) = "en-" {
PreferredLanguage := True
PreferredOCRLanguage := A_LoopField
Break
}
}
If FirstAvailableLanguage = False And PreferredLanguage = False {
Sleep 25
Speak("No OCR languages installed!")
}
Else {
If PreferredLanguage = False
OCRLanguage := FirstOCRLanguage
Else
OCRLanguage := PreferredOCRLanguage
OCRLines := Array()
OCRResult := OCR.FromWindow("A", OCRLanguage,, 1)
For OCRLine In OCRResult.Lines {
OCRLines.Push(Map("Text", OCRLine.Text, "X1", OCR.WordsBoundingRect(OCRLine.Words*).X, "Y1", OCR.WordsBoundingRect(OCRLine.Words*).Y, "X2", OCR.WordsBoundingRect(OCRLine.Words*).X + OCR.WordsBoundingRect(OCRLine.Words*).W, "Y2", OCR.WordsBoundingRect(OCRLine.Words*).Y + OCR.WordsBoundingRect(OCRLine.Words*).H))
}
ClipboardData := ""
ConfirmationDialog := MsgBox("Compensate for the position of the currently focused control?", AppName, 4)
If ConfirmationDialog == "Yes" {
Sleep 1000
If ControlClass = False {
Speak("Focused control not found")
}
Else {
ClipboardData .= "Compensating for X " . ControlX . ", Y " . ControlY . "`r`n"
For Value In OCRLines {
Text := Value["Text"]
X1Coordinate := Value["X1"] - ControlX
Y1Coordinate := Value["Y1"] - ControlY
X2Coordinate := Value["X2"] - ControlX
Y2Coordinate := Value["Y2"] - ControlY
ClipboardData .= "`"" . Text . "`"`r`nBeginning at X " . X1Coordinate . ", Y " . Y1Coordinate . "`r`nEnding at X " . X2Coordinate . ", Y " . Y2Coordinate . "`r`n`r`n"
}
}
}
Else {
Sleep 1000
For Value In OCRLines {
Text := Value["Text"]
X1Coordinate := Value["X1"]
Y1Coordinate := Value["Y1"]
X2Coordinate := Value["X2"]
Y2Coordinate := Value["Y2"]
ClipboardData .= "`"" . Text . "`"`r`nBeginning at X " . X1Coordinate . ", Y " . Y1Coordinate . "`r`nEnding at X " . X2Coordinate . ", Y " . Y2Coordinate . "`r`n`r`n"
}
}
ClipboardData := RTrim(ClipboardData, "`r`n")
A_Clipboard := ClipboardData
If OCRLines.Length = 1
Speak("1 OCR line copied to clipboard")
Else
Speak(OCRLines.Length . " OCR lines copied to clipboard")
}
}
DialogOpen := 0
}
}
Quit(*) {
Global AppName, DialogOpen
If DialogOpen = 0 {
DialogOpen := 1
ConfirmationDialog := MsgBox("Are you sure you want to quit the app?", "Quit " . AppName, 4)
If ConfirmationDialog == "Yes" {
ExitApp
}
DialogOpen := 0
}
}
RenameHotspot() {
Global AppName, CurrentHotspot, DialogOpen, Hotspots
If DialogOpen = 0
If Hotspots.Length > 0 And CurrentHotspot > 0 And CurrentHotspot <= Hotspots.Length {
DialogOpen := 1
RenameDialog := InputBox("Enter a new name for this hotspot.", AppName, "", Hotspots[CurrentHotspot]["Label"])
If RenameDialog.Result == "OK" And Not RenameDialog.Value = "" {
Hotspots[CurrentHotspot]["Label"] := RenameDialog.Value
Sleep 25
Speak(Hotspots[CurrentHotspot]["Label"])
}
DialogOpen := 0
}
Else {
Sleep 25
Speak("No Hotspot Selected")
}
}
ReportMousePosition() {
Global DialogOpen
If DialogOpen = 0 {
MouseGetPos &MouseXCoordinate, &MouseYCoordinate
Speak("X " . MouseXCoordinate . " Y " . MouseYCoordinate)
}
}
ReportPixelColor() {
Global MouseXPosition, MouseYPosition
MouseGetPos &mouseXPosition, &mouseYPosition
Speak(PixelGetColor(MouseXPosition, MouseYPosition, "Slow"))
}
RouteMouseToFocusedControl() {
Global AppName, DialogOpen
If DialogOpen = 0 {
DialogOpen := 1
Try {
WinWaitActive("A")
ControlClass := ControlGetClassNN(ControlGetFocus("A"))
}