Loading...
·-

摩斯电码转换器

将文本转换为摩斯电码,或将摩斯电码还原为文本,支持音频播放

输入文本

0 字符

摩斯电码

0 字符
可视化显示区域
🔊 播放速度:

📖 摩斯电码对照表

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
----·
: '...-..-', '@': '.--.-.', ' ': '/' }; const REVERSE_MORSE = Object.fromEntries( Object.entries(MORSE_CODE).map(([k, v]) => [v, k]) ); let audioContext = null; let isPlaying = false; let playTimer = null; const inputArea = document.querySelector('.openskil-input'); const outputArea = document.querySelector('.openskil-output'); const visualArea = document.querySelector('.openskil-visual-area'); const inputCount = document.querySelector('.openskil-input-count'); const outputCount = document.querySelector('.openskil-output-count'); const playBtn = document.querySelector('.openskil-btn-play-start'); const speedSelect = document.querySelector('.openskil-speed-value'); const toast = document.querySelector('.openskil-toast-message'); function showToast(message) { toast.textContent = message; toast.classList.add('show'); setTimeout(() => toast.classList.remove('show'), 2500); } function updateCharCount() { inputCount.textContent = `${inputArea.value.length} 字符`; outputCount.textContent = `${outputArea.value.length} 字符`; } function updateVisual(morseCode) { if (!morseCode || morseCode.trim() === '') { visualArea.innerHTML = '可视化显示区域'; return; } let html = ''; for (const char of morseCode) { if (char === '.') { html += ''; } else if (char === '-') { html += ''; } else if (char === ' ') { html += '   '; } else if (char === '/') { html += '    '; } } visualArea.innerHTML = html; } function textToMorse(text) { return text.toUpperCase().split('').map(char => { if (MORSE_CODE[char]) return MORSE_CODE[char]; if (char === ' ' || char === '\n') return char; return ''; }).join(' ').replace(/ +/g, ' ').trim(); } function morseToText(morse) { return morse.split(' ').map(code => { if (code === '/') return ' '; return REVERSE_MORSE[code] || ''; }).join(''); } function convertToMorse() { const text = inputArea.value.trim(); if (!text) { showToast('请输入要转换的文本'); return; } const morse = textToMorse(text); outputArea.value = morse; updateCharCount(); updateVisual(morse); showToast('转换成功!'); } function convertToText() { const morse = inputArea.value.trim(); if (!morse) { showToast('请输入要转换的摩斯电码'); return; } const normalized = morse.replace(/·/g, '.').replace(/—/g, '-'); const text = morseToText(normalized); outputArea.value = text; updateCharCount(); updateVisual(''); showToast('还原成功!'); } function initAudio() { if (!audioContext) { audioContext = new (window.AudioContext || window.webkitAudioContext)(); } } function playTone(duration) { initAudio(); const oscillator = audioContext.createOscillator(); const gainNode = audioContext.createGain(); oscillator.connect(gainNode); gainNode.connect(audioContext.destination); oscillator.frequency.value = 600; oscillator.type = 'sine'; gainNode.gain.setValueAtTime(0.3, audioContext.currentTime); gainNode.gain.exponentialRampToValueAtTime(0.01, audioContext.currentTime + duration / 1000); oscillator.start(audioContext.currentTime); oscillator.stop(audioContext.currentTime + duration / 1000); } function getSpeedConfig() { const speed = speedSelect.value; const configs = { fast: { dot: 60, dash: 180, letterSpace: 60, wordSpace: 180 }, normal: { dot: 100, dash: 300, letterSpace: 100, wordSpace: 300 }, slow: { dot: 150, dash: 450, letterSpace: 150, wordSpace: 450 } }; return configs[speed]; } function playMorse() { const morse = outputArea.value.trim(); if (!morse) { showToast('请先转换文本为摩斯电码'); return; } if (isPlaying) { stopMorse(); return; } initAudio(); isPlaying = true; playBtn.textContent = '⏹ 停止播放'; playBtn.classList.add('openskil-btn-stop'); const speed = getSpeedConfig(); const sequence = []; for (let i = 0; i < morse.length; i++) { const char = morse[i]; if (char === '.') { sequence.push({ type: 'tone', duration: speed.dot }); } else if (char === '-') { sequence.push({ type: 'tone', duration: speed.dash }); } else if (char === ' ') { if (i > 0 && morse[i - 1] !== ' ') { sequence.push({ type: 'silent', duration: speed.letterSpace }); } } else if (char === '/') { sequence.push({ type: 'silent', duration: speed.wordSpace }); } } let index = 0; const playNext = () => { if (!isPlaying || index >= sequence.length) { stopMorse(); return; } const item = sequence[index]; if (item.type === 'tone') { playTone(item.duration); } index++; playTimer = setTimeout(playNext, item.duration + 50); }; playNext(); } function stopMorse() { isPlaying = false; if (playTimer) { clearTimeout(playTimer); playTimer = null; } playBtn.textContent = '▶ 播放音频'; playBtn.classList.remove('openskil-btn-stop'); } function copyResult() { const text = outputArea.value; if (!text) { showToast('没有内容可复制'); return; } navigator.clipboard.writeText(text).then(() => { showToast('已复制到剪贴板'); }).catch(() => { outputArea.select(); document.execCommand('copy'); showToast('已复制到剪贴板'); }); } inputArea.addEventListener('input', updateCharCount); inputArea.addEventListener('keydown', (e) => { if (e.key === 'Enter' && e.ctrlKey) { convertToMorse(); } });
操作面板
工具详情
  • 工具图片:
  • 工具名称: 摩斯电码转换器 - 在线文本与摩斯电码互转工具
  • 创建时间: Thu Apr 09 2026 20:10:25 GMT+0800 (China Standard Time)
  • 收藏数量: 共0人
  • 点赞数量: 共0次
  • 分享次数: 共0次
  • 访问数量: 共0次
  • 工具版本: v1.0.0
会员评论

主标题

站点所有消息通知及提示Tips内容!