我正在尝试在BATCH中模拟打字效果,这就是我最初拥有的:
@echo off
setlocal enabledelayedexpansion
set "word=example"
set "output="
for /l %%x in (0, 1, 6) do (
set "output=!output!!word:~%%x,1!"
cls
echo !output!_
ping localhost -n 1 -w 200 >nul
)
cls
echo !output!
pause
但是,这不起作用,只是完全回显该单词,而不模拟任何打字效果。有什么解决办法吗?
尝试过:上面的代码
预期:简单、清晰的打字效果,可以打出单词。
结果:只是回显了这个词,并没有真正模拟出打字效果。
使用 while 循环和一些简单的数学来跟踪经过的厘秒可用于实现此类输出的适当持续时间的延迟。
@echo off & setlocal enabledelayedexpansion
CLS
(Set \n=^^^
%= do not modify this multiline \n definition =%)
rem @wait macro by T3RRY - wait approximately n centiseconds
rem usage: %@wait% n
rem usage: %@wait% "equation"
Set @wait=For %%. in (1 2)Do if %%.==2 (%\n%
Set "$wait.do=1"%\n%
For /l %%^^~ in (1 1 16)Do if defined $wait.do For /l %%^^~ in (1 1 16)Do if defined $wait.do For /l %%^^~ in (1 1 16)Do if defined $wait.do For /l %%^^~ in (1 1 16)Do if defined $wait.do (%\n%
For /f "tokens=1,* Delims= " %%i in ("$waitamount ^[email protected]:* =^!")Do If not "^!%%i.CS^!"=="^!time:~-1^!" (%\n%
Set /A "@wait.Target=%%~j","@wait.tDiff=^!time:~-1^!-%%i.CS","%%i.CS=^!time:~-1^!"%\n%
If "^[email protected]:~0,-1^!"=="-" (%\n%
Set /A "@wait.tDiff+=10,%%i.elapsed=%%i.elapsed %% [email protected]"%\n%
)Else Set /A "%%i.elapsed=%%i.elapsed %% [email protected]"%\n%
If ^^!%%i.elapsed^^! GEQ ^^[email protected]^^! (%\n%
Set /a "@wait.tDiff=0,%%i.elapsed=0"%\n%
Set "$wait.do="%\n%
)%\n%
)%\n%
)%\n%
) Else Set @wait.Args=
rem @strlen macro - port of strlen function by Jeb. rem returns the length of a string.
rem usage: %@strlen% sourceVar returnVar
rem https://www.dostips.com/forum/viewtopic.php?f=3&t=1429&start=15#p6267
Set @strlen=For %%. in (1 2)Do if %%.==2 (%\n%
For /f "tokens=1,2" %%i in ("^[email protected]^!")Do (%\n%
Set "@strlen.t=^!%%~i^!"%\n%
If defined @strlen.t (%\n%
Set "%%j=1"%\n%
For %%P in ( 4096 2048 1024 512 256 128 64 32 16 8 4 2 1 )Do (%\n%
If not "^[email protected]:~%%P,1^!" == "" (%\n%
Set /a "%%~j+=%%P"%\n%
Set "@strlen.t=^[email protected]:~%%P^!"%\n%
)%\n%
)%\n%
)Else set "%%~j=0"%\n%
)%\n%
) Else Set @strlen.Args=
Set "string=hello world^!"
%@strlen% string $len
rem vt sequence used to facilitate output of whitespace and = character via set /p.
rem vt sequences require windows 10 build 10589 or newer.
for /F %%a in ('Echo(prompt $E^| %comspec%')Do Set \E=%%a
For /l %%i in (0 1 !$len!)do (
<nul set /p "=%\E%#5!string:~%%i,1!"
%@wait% "!random! %% 8 + (!random! %% 6 + 6)"
)
echo(
Pause
Endlocal & goto:eof
有关带有参数的宏的信息,请参阅:https://www.dostips.com/forum/viewtopic.php?t=1827
有关虚拟终端序列的信息,请参阅:https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences