自动输入脚本:解决无法复制粘贴时的文本传输难题
在某些办公场景中,受限于安全策略或系统隔离,我们无法使用复制文件、粘贴文本、共享剪切板等方式,只能手动将代码或文字从公共区域输入到受控系统中。这种做法非常低效,容易出错。
为此,我编写了一个基于 AutoHotkey 的自动输入脚本,它能够模拟键盘逐字输入,将文件内容“打字”到目标窗口,极大提高效率。
使用场景
- 无法使用 U 盘/共享文件夹传输内容
- 剪贴板被完全屏蔽(禁用 Ctrl+C / Ctrl+V)
- 只允许“肉眼抄写”的受控终端
脚本功能
- 自动读取指定文件内容
- 模拟键盘输入,一字一字打出
- 识别换行、Shift+字符、大写字母等特殊情况
- 可配置延时,避免输入错误
核心脚本(AutoHotkey 版)
#NoEnv
#SingleInstance Force
SendMode Input
FileEncoding, UTF-8
SetKeyDelay, 0, 20
; ===== Config =====
SleepDelay := 3000
NormalDelay := 100
ShiftedDelay := 300
DefaultFile := "D:\miniAlu_tb.v"
; ==================
IfExist, %DefaultFile%
FilePath := DefaultFile
else {
FileSelectFile, FilePath, 3,, Select a text file, Text Files (*.txt)
if (FilePath = "") {
MsgBox, No file selected.
ExitApp
}
}
Sleep, %SleepDelay%
FileRead, content, %FilePath%
if ErrorLevel {
MsgBox, Failed to read file: %FilePath%
ExitApp
}
; Normalize line endings
StringReplace, content, content, `r`n, `n, All
StringReplace, content, content, `r, , All
; Define mapping for shifted characters
shiftedChars := {}
shiftedChars["!"] := "1"
shiftedChars["@"] := "2"
shiftedChars["#"] := "3"
shiftedChars["$"] := "4"
shiftedChars["%"] := "5"
shiftedChars["^"] := "6"
shiftedChars["&"] := "7"
shiftedChars["*"] := "8"
shiftedChars["("] := "9"
shiftedChars[")"] := "0"
shiftedChars["_"] := "-"
shiftedChars["+"] := "="
shiftedChars["{"] := "["
shiftedChars["}"] := "]"
shiftedChars[":"] := ";"
shiftedChars["\\"] := "|"
shiftedChars["<"] := ","
shiftedChars[">"] := "."
shiftedChars["?"] := "/"
shiftedChars["~"] := "`"
shiftedChars[""""] := "'"
Loop, Parse, content
{
char := A_LoopField
if (char = "`n") {
Send, {Enter}
Sleep, %NormalDelay%
}
else if (char ~= "^[A-Z]$") {
Send, {Shift down}
Sleep, %NormalDelay%
Send, %char%
Sleep, %NormalDelay%
Send, {Shift up}
Sleep, %NormalDelay%
}
else if (shiftedChars.HasKey(char)) {
baseChar := shiftedChars[char]
Send, {Shift down}
Sleep, %NormalDelay%
Send, %baseChar%
Sleep, %NormalDelay%
Send, {Shift up}
Sleep, %NormalDelay%
}
else {
Send, %char%
Sleep, %NormalDelay%
}
}
MsgBox, Typing completed.
ExitApp
使用说明
- 安装 AutoHotkey
- 将上述脚本保存为
.ahk文件(例如auto_typing.ahk) - 双击运行脚本
- 程序会在 3 秒后开始模拟打字,请切换到目标窗口(如 VSCode、终端等)
- 输入完成后弹窗提示
注意事项
- 请确保目标窗口处于可编辑状态,并有输入焦点
- 输入过程中请勿移动光标或中断
- 如果有特殊字符未正确输入,可手动扩展
shiftedChars表 - 可适当调整
NormalDelay和ShiftedDelay以平衡速度与稳定性