|
1 |
| -# My Auto Hot Key Practice |
| 1 | +# [My Auto Hot Key Practice](../README.md#my-auto-hot-key-practice) |
| 2 | + |
| 3 | +Boys be ambitious …… ! |
| 4 | + |
2 | 5 |
|
3 | 6 | ### \<List>
|
| 7 | + |
| 8 | +- [GUI Calculator (2024.01.13)](#gui-calculator-20240113) |
4 | 9 | - [Hello World (2022.09.03)](#hello-world-20220903)
|
5 | 10 | - [Infinity (2021.12.07)](#infinity-20211207)
|
6 | 11 |
|
7 | 12 |
|
| 13 | +## [GUI Calculator (2024.01.13)](#list) |
| 14 | + |
| 15 | +- A Practice of GUI coding with *AutoHotKey* |
| 16 | + - Can choose Real / Test mode |
| 17 | + |
| 18 | +  |
| 19 | + |
| 20 | +  |
| 21 | + |
| 22 | +- Future Improvements |
| 23 | + - Improved design: Add colors, fonts, etc. |
| 24 | + - When `A opr. B opr.` is entered, immediately provide calculation results of `A opr. B` |
| 25 | + |
| 26 | +- Code |
| 27 | + <details> |
| 28 | + <summary>GUICalculator.ahk</summary> |
| 29 | + |
| 30 | + ```ahk |
| 31 | + ; Test mode |
| 32 | +
|
| 33 | + Test := false |
| 34 | + ``` |
| 35 | + ```ahk |
| 36 | + ; Set GUI |
| 37 | +
|
| 38 | + if (Test = true) |
| 39 | + { |
| 40 | + Gui, Add, Text, x2 y2 w200 h20 vDisplayText, ; Display box |
| 41 | + Gui, Add, Text, x2 y22 w200 h20 vDebugText, ; Debug box |
| 42 | + } |
| 43 | + else |
| 44 | + { |
| 45 | + Gui, Font, s20 |
| 46 | + Gui, Add, Text, x12 y9 w180 h40 vDisplayText, ; Display box |
| 47 | + } |
| 48 | +
|
| 49 | + Gui, Font, s16 |
| 50 | + Gui, Add, Button, x2 y49 w50 h50 gBtn, 7 |
| 51 | + Gui, Add, Button, x52 y49 w50 h50 gBtn, 8 |
| 52 | + Gui, Add, Button, x102 y49 w50 h50 gBtn, 9 |
| 53 | + Gui, Add, Button, x152 y49 w50 h50 gBtn, + |
| 54 | +
|
| 55 | + Gui, Add, Button, x2 y99 w50 h50 gBtn, 4 |
| 56 | + Gui, Add, Button, x52 y99 w50 h50 gBtn, 5 |
| 57 | + Gui, Add, Button, x102 y99 w50 h50 gBtn, 6 |
| 58 | + Gui, Add, Button, x152 y99 w50 h50 gBtn, - |
| 59 | +
|
| 60 | + Gui, Add, Button, x2 y149 w50 h50 gBtn, 1 |
| 61 | + Gui, Add, Button, x52 y149 w50 h50 gBtn, 2 |
| 62 | + Gui, Add, Button, x102 y149 w50 h50 gBtn, 3 |
| 63 | + Gui, Add, Button, x152 y149 w50 h50 gBtn, * |
| 64 | +
|
| 65 | + Gui, Add, Button, x2 y199 w50 h50 gBtn cRed, C |
| 66 | + Gui, Add, Button, x52 y199 w50 h50 gBtn, 0 |
| 67 | + Gui, Add, Button, x102 y199 w50 h50 gBtn, = |
| 68 | + Gui, Add, Button, x152 y199 w50 h50 gBtn, / |
| 69 | +
|
| 70 | + Gui, Show, w204 h250, Calculator ; Control the window size |
| 71 | + ``` |
| 72 | + ```ahk |
| 73 | + ; Decalre variables |
| 74 | +
|
| 75 | + Num1 := "" |
| 76 | + Num2 := "" |
| 77 | + Operator := "" |
| 78 | + Answer := 0 |
| 79 | + JustAnswered := false |
| 80 | + EMPTY := "" |
| 81 | +
|
| 82 | + return |
| 83 | + ``` |
| 84 | + ```ahk |
| 85 | + Btn: |
| 86 | +
|
| 87 | + CurrentText := A_GuiControl ; A_GuiControl : Get the text on the each button |
| 88 | +
|
| 89 | + if (CurrentText = "C") ; `C` : Reset |
| 90 | + { |
| 91 | + GuiControl,, DisplayText, |
| 92 | + Num1 := "" |
| 93 | + Num2 := "" |
| 94 | + Answer := 0 |
| 95 | + Operator := "" |
| 96 | + JustAnswered := false |
| 97 | + DisplayText := "" |
| 98 | + } |
| 99 | + else if (CurrentText = "=") ; `=` : Do operation |
| 100 | + { |
| 101 | + if (Num2 != EMPTY and Operator != EMPTY) ; "" can be used only with `:=`! |
| 102 | + { |
| 103 | + Num1 := Num1 + 0 ; Convert `Num1`, `Num2` from String to Integer |
| 104 | + Num2 := Num2 + 0 |
| 105 | +
|
| 106 | + if (Operator = "+") |
| 107 | + Answer := Num1 + Num2 |
| 108 | + else if (Operator = "-") |
| 109 | + Answer := Num1 - Num2 |
| 110 | + else if (Operator = "*") |
| 111 | + Answer := Num1 * Num2 |
| 112 | + else ; The same with `else if (Operator = "/")` |
| 113 | + Answer := Num1 / Num2 |
| 114 | +
|
| 115 | + GuiControl,, DisplayText, % Answer ; <Answer> |
| 116 | + ;~ MsgBox, % "Answer : " Answer ; Ok |
| 117 | + Num1 := Answer ; Succeed `Answer` through `Num1` |
| 118 | + Num2 := "" ; and be ready to get `Num2` for the new operation |
| 119 | + Operator := "" |
| 120 | + JustAnswered := true |
| 121 | + } |
| 122 | + ; Do nothing when `Num2` and `Operator` are empty |
| 123 | + } |
| 124 | + else if InStr("+-*/", CurrentText) ; `+-*/` : Determine the kind of operation |
| 125 | + { |
| 126 | + if (Num1 != EMPTY) |
| 127 | + { |
| 128 | + Operator := CurrentText |
| 129 | + JustAnswered := false |
| 130 | + if (Num2 != EMPTY) |
| 131 | + Num2 := "" |
| 132 | +
|
| 133 | + GuiControl,, DisplayText, % DisplayText := Num1 " " Operator " " ; Append `Operator` into `DisplayText` |
| 134 | + } |
| 135 | + ; Do nothing when `Num1` is still empty |
| 136 | + } |
| 137 | + else ; `0~9` : Get a number |
| 138 | + { |
| 139 | + if (Operator = EMPTY) |
| 140 | + { |
| 141 | + if (JustAnswered) |
| 142 | + { |
| 143 | + Num1 := "" |
| 144 | + Answer := 0 |
| 145 | + JustAnswered := false |
| 146 | + DisplayText := "" |
| 147 | + } |
| 148 | +
|
| 149 | + Num1 .= CurrentText |
| 150 | + } |
| 151 | + else |
| 152 | + Num2 .= CurrentText |
| 153 | +
|
| 154 | + GuiControl,, DisplayText, % DisplayText .= CurrentText ; Append `CurrentText` into `DisplayText` |
| 155 | + } |
| 156 | +
|
| 157 | + if (Test = true) |
| 158 | + GuiControl,, DebugText, % CurrentText " / " JustAnswered " / " Num1 " " Operator " " Num2 " = " Answer |
| 159 | +
|
| 160 | + return |
| 161 | + ``` |
| 162 | + ```ahk |
| 163 | + GuiClose: |
| 164 | +
|
| 165 | + ExitApp |
| 166 | + ``` |
| 167 | + </details> |
| 168 | + |
| 169 | + |
8 | 170 | ## [Hello World (2022.09.03)](#list)
|
9 | 171 |
|
10 | 172 | - It is so simple but somewhat crazy that `LButton` always calls the message box ……
|
11 | 173 |
|
12 |
| - |
| 174 | +  |
13 | 175 |
|
14 |
| -#### `HelloWorld.ahk` |
| 176 | + #### `HelloWorld.ahk` |
15 | 177 |
|
16 |
| -```ahk |
17 |
| -LButton:: |
18 |
| - MsgBox, Hello World! |
| 178 | + ```ahk |
| 179 | + LButton:: |
| 180 | + MsgBox, Hello World! |
19 | 181 |
|
20 |
| -return |
21 |
| -``` |
| 182 | + return |
| 183 | + ``` |
22 | 184 |
|
23 | 185 | ## [Infinity (2021.12.07)](#list)
|
24 | 186 |
|
25 | 187 | - Welcome to `Auto Hot Key` world of mystery!
|
26 | 188 |
|
27 |
| -#### `Infinity.ahk` |
28 |
| - |
29 |
| -```ahk |
30 |
| -; Test 1 |
31 |
| -if (1 == 2) |
32 |
| -{ |
33 |
| - MsgBox, True |
34 |
| -} |
35 |
| -else MsgBox, False |
36 |
| -``` |
37 |
| -> False |
38 |
| -
|
39 |
| -```ahk |
40 |
| -; Test 2 |
41 |
| -if (11111111111111111111111 == 22222222222222222222222) |
42 |
| -{ |
43 |
| - MsgBox, True |
44 |
| -} |
45 |
| -else MsgBox, False |
46 |
| -``` |
47 |
| -> True |
| 189 | + #### `Infinity.ahk` |
| 190 | + |
| 191 | + ```ahk |
| 192 | + ; Test 1 |
| 193 | + if (1 == 2) |
| 194 | + { |
| 195 | + MsgBox, True |
| 196 | + } |
| 197 | + else MsgBox, False |
| 198 | + ``` |
| 199 | + > False |
| 200 | +
|
| 201 | + ```ahk |
| 202 | + ; Test 2 |
| 203 | + if (11111111111111111111111 == 22222222222222222222222) |
| 204 | + { |
| 205 | + MsgBox, True |
| 206 | + } |
| 207 | + else MsgBox, False |
| 208 | + ``` |
| 209 | + > True |
0 commit comments