Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.2 kB
2
Indexable
Never
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

; define the Morse code alphabet
MorseAlphabet := {
    A: ".-",
    B: "-...",
    C: "-.-.",
    D: "-..",
    E: ".",
    F: "..-.",
    G: "--.",
    H: "....",
    I: "..",
    J: ".---",
    K: "-.-",
    L: ".-..",
    M: "--",
    N: "-.",
    O: "---",
    P: ".--.",
    Q: "--.-",
    R: ".-.",
    S: "...",
    T: "-",
    U: "..-",
    V: "...-",
    W: ".--",
    X: "-..-",
    Y: "-.--",
    Z: "--..",
    "1": ".----",
    "2": "..---",
    "3": "...--",
    "4": "....-",
    "5": ".....",
    "6": "-....",
    "7": "--...",
    "8": "---..",
    "9": "----.",
    "0": "-----",
    " ": " ",
}

; define the text-to-Morse code conversion function
TextToMorse(text)
{
    morseText := ""
    Loop, Parse, text
    {
        char := A_LoopField
        morseChar := MorseAlphabet[char]
        morseText .= morseChar
    }
    return morseText
}

; define the Morse code-to-text conversion function
MorseToText(morse)
{
    text := ""
    Loop, Parse, morse, ` `
    {
        morseChar := A_LoopField
        char := ""
        Loop, % MorseAlphabet.MaxIndex()
        {
            if (MorseAlphabet[A_Index] = morseChar)
            {
                char := A_Index
                break
            }
        }
        text .= char
    }
    return text
}

; create a GUI window
Gui, Add, Text,, Enter text or Morse code:
Gui, Add, Edit, vInput
Gui, Add, Button, Default, Convert
Gui, Add, Text,, Converted text or Morse code:
Gui, Add, Edit, vOutput ReadOnly
Gui, Show

; define the GUI event loop
GuiEvent:
input := GuiControlGet(Input)
output := ""
if (input = "")
{
    output := "Please enter some text or Morse code."
}
else if (RegExMatch(input, "[^-. ]"))
{
    output := TextToMorse(input)
}
else
{
    output := MorseToText(input)
}
Gui