[AHK] Convert characters to Morse code

This program can be used in Notepad. When the user presses the space bar, the program will get the currently selected text and convert it into Morse code and play it. If the text selected by the user contains non-Morse code characters, the program will automatically ignore them.

Convert characters to Morse code

; 这个程序可以在记事本中使用。当用户按下空格键时,程序会获取当前选中的文本,并将其转换为摩尔斯电码播放出来。如果用户选中的文本包含非摩尔斯电码字符,程序会自动忽略它们。
#SingleInstance, Force

; 定义摩尔斯电码对应表

global morseCode := {"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": "--..", "0": "-----", "1": ".----", "2": "..---", "3": "...--", "4": "....-", "5": ".....", "6": "-....", "7": "--...", "8": "---..", "9": "----.", ".": ".-.-.-", ",": "--..--", "?": "..--..", "'": ".----.", "/": "-..-.", "(": "-.--.", ")": "-.--.-", "&": ".-...", ":": "---...", ";": "-.-.-.", "=": "-...-", "+": ".-.-.", "-": "-....-", "_": "..--.-", """": ".-..-."}

; 定义摩尔斯电码播放函数
PlayMorseCode(code) {
	; 将电码转换为摩尔斯电码

	morse := ""
	loop, Parse, code
	{
		; MsgBox % StrUpper(A_LoopField)
		; MsgBox % morseCode["A"]
		morse .= morseCode[StrUpper(A_LoopField)]
	}
	ToolTip %code%`n%morse%

	; 播放摩尔斯电码
	loop, Parse, morse
	{
		key := A_LoopField
		if (key = ".") {
			SoundBeep, 500, 100
		}
		else if (key = "-")
		{
			SoundBeep, 500, 300
		}
		else
		{
			Sleep, 300
		}
		Sleep, 100
	}
}
StrUpper(ByRef InputVar, T = "") {
	StringUpper, v, InputVar, %T%
	return, v
}
; 监听按键
#IfWinActive ahk_exe notepad.exe
$*Space::
	; 获取当前选中的文本
	Clipboard := ""
	Send, ^c
	ClipWait
	text := Clipboard
	; ToolTip %text%
	; 播放摩尔斯电码
	PlayMorseCode(text)
return
#IfWinActive

Guess you like

Origin blog.csdn.net/liuyukuan/article/details/130307243