Microsoft Small Basic

Program Listing: NHT122
' Morse Telegraph Key
' Version 0.1a
' Copyright © 2019 Nonki Takahashi. The MIT License.

GraphicsWindow.Title = "Morse Telegraph Key 0.1a"
LF = Text.GetCharacter(10)
Form()
t0 = Clock.ElapsedMilliseconds
GraphicsWindow.KeyDown = OnKeyDown
GraphicsWindow.KeyUp = OnKeyUp
Controls.ButtonClicked = OnButtonClicked

Sub Form
gw = 598
gh = 428
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
GraphicsWindow.BackgroundColor = "LightGray"
GraphicsWindow.PenWidth = 0
GraphicsWindow.BrushColor = "Blue"
lamp = Shapes.AddRectangle(gw, gh)
Shapes.SetOpacity(lamp, 80)
Shapes.HideShape(lamp)
GraphicsWindow.BrushColor = "Black"
tbox = Controls.AddMultiLineTextBox(10, 10)
th = 250
Controls.SetSize(tbox, gw - 20, th)
btn = Controls.AddButton("Reset", gw - 54, gh - 34)
morse = ".-=A;-...=B;-.-.=C;-..=D;.=E;"
morse = morse + "..-.=F;--.=G;....=H;..=I;.---=J;"
morse = morse + "-.-=K;.-..=L;--=M;-.=N;---=O;"
morse = morse + ".--.=P;--.-=Q;.-.=R;...=S;-=T;"
morse = morse + "..-=U;...-=V;.--=W;-..-=X;-.--=Y;--..=Z;"
morse = morse + ".----=1;..---=2;...--=3;....-=4;.....=5;"
morse = morse + "-....=6;--...=7;---..=8;----.=9;-----=0;"
GraphicsWindow.FontName = "Consolas"
index = Array.GetAllIndices(morse)
x = 10
y = th + 20
For c = 1 To Array.GetItemCount(morse)
GraphicsWindow.DrawText(x, y, morse[index[c]] + " " + index[c])
If Text.IsSubText("(7)(14)(21)(26)(31)", "(" + c + ")") Then
x = 10
y = y + 20
Else
x = x + 82
EndIf
If c = 26 Then
y = y + 20
EndIf
EndFor
EndSub

Sub OnButtonClicked
i = 0
t0 = Clock.ElapsedMilliseconds
buf = ""
Controls.SetTextBoxText(tbox, buf)
EndSub

Sub OnKeyDown
t1 = Clock.ElapsedMilliseconds
Shapes.ShowShape(lamp)
d[i] = Math.Round(t1 - t0)
buf = buf + "(" + d[i] + ") "
i = i + 1
If Math.Remainder(i, 18) = 0 Then
buf = buf + LF
EndIf
Controls.SetTextBoxText(tbox, buf)
keyDown = "True"
EndSub

Sub OnKeyUp
t0 = Clock.ElapsedMilliseconds
Shapes.HideShape(lamp)
d[i] = Math.Round(t0 - t1)
buf = Text.Append(buf, d[i]) + " "
i = i + 1
If Math.Remainder(i, 18) = 0 Then
buf = buf + LF
EndIf
Controls.SetTextBoxText(tbox, buf)
keyUp = "True"
EndSub