Batch File: Typewriter Effect

后端 未结 4 1397
無奈伤痛
無奈伤痛 2020-12-06 23:38

I just got some code to do this effect, but I need to it to type faster, like a letter every half a second.

for %%i in (h e l l o o o o o o o o o o o o o o)          


        
相关标签:
4条回答
  • 2020-12-06 23:41

    Just give a try for this code : Message Typewriter + Speaking Voice

    @echo off
    Title Typewriter with speaking voice by Hackoo 2016
    Color 0A & Mode con cols=70 lines=3
    set Msg="This is only a testing string to see if my script really works or not?"
    Call :Typewriter %Msg%
    set Msg="              What are you doing now dude ?"
    Call :Typewriter %Msg%
    set Msg="         I want to say Hello for everybody on StackOverflow !"
    Call :Typewriter %Msg%
    pause>nul
    Exit /b
    ::*********************************************************************
    :TypeWriter
    Cls
    echo(
    (
    echo strText=wscript.arguments(0^)
    echo intTextLen = Len(strText^)
    echo intPause = 150
    echo For x = 1 to intTextLen
    echo     strTempText = Mid(strText,x,1^)
    echo     WScript.StdOut.Write strTempText
    echo     WScript.Sleep intPause
    echo Next
    echo Set Voice=CreateObject("SAPI.SpVoice"^)
    echo voice.speak strText
    )>%tmp%\%~n0.vbs
    @cScript.EXE /noLogo "%tmp%\%~n0.vbs" "%~1"
    exit /b
    ::********************************************************************* 
    
    0 讨论(0)
  • 2020-12-06 23:50

    Pinging 0.0.0.0 gives PING: transmit failed. General failure error message nearly immediately.

    (Edited according to suggestive rojo's comment)

    Better to ping some address returning Request timed out message chosen from a Private-Use Network range.

    for %%i in (h e l l o o o o o o o o o o o o o o) do (
       set /p a=%%i<nul
       ping a.b.c.d -4 -n 1 -w 500>nul 2>&1
    )
    

    Here:

    • a.b.c.d = address returning Request timed out message, see (*) below.

    • -4 = force using IPv4.

    • -n 1 = number of echo requests to send (one).

    • -w 500 = timeout in milliseconds to wait for each reply (500=1/2 sec); this should work to cca 5 seconds (approximate time for one unsuccesfull echo request).

    (*) The following IPv4 address ranges have been reserved by the IANA for private internets, and are not publicly routable on the global internet:

     10.0.0.0     --   10.255.255.255  (10/8       prefix)
     172.16.0.0   --   172.31.255.255  (172.16/12  prefix)
     192.168.0.0  --   192.168.255.255 (192.168/16 prefix)
    
    0 讨论(0)
  • 2020-12-06 23:54
    @echo off
    setlocal enableextensions enabledelayedexpansion
    
    set lines=1
    
    
    set "line1=Check it out M8! Im typing but not typing! BEWM! BEWM! BEWM!"
    
    
    for /f %%a in ('"prompt $H&for %%b in (1) do rem"') do set "BS=%%a"
    
    for /L %%a in (1,1,%lines%) do set num=0&set "line=!line%%a!"&call :type
    
    pause>nul
    goto :EOF
    
    :type
    set "letter=!line:~%num%,1!"
    set "delay=%random%%random%%random%%random%%random%%random%%random%"
    set "delay=%delay:~-6%"
    if not "%letter%"=="" set /p "=a%bs%%letter%" <nul
    
    :: adjust the 3 in the line below: higher is faster typing speed
    
    for /L %%b in (1,3,%delay%) do rem
    if "%letter%"=="" echo.&goto :EOF
    set /a num+=1
    goto :type
    
    0 讨论(0)
  • 2020-12-06 23:59

    The ping solution in the comments above is a worthwhile solution for computers with installed network interfaces (as almost all have).

    @echo off
    setlocal
    
    for %%i in (h e l l o o o o o o o o o o o o o o) do (
       set /p "=%%i"<nul
       ping 169.254.0.0 -n 1 -w 500 >nul
    )
    echo;
    goto :EOF
    

    However, it appears that the minimum wait time recognized with this method is 500ms. If you change the 500 to a lower value, you still pause a half second between letters. If you want finer control, or if your computer has no network interface, you'll have to borrow from another runtime environment -- JScript, for example.

    @if (@CodeSection == @Batch) @then
    
    @echo off
    setlocal
    
    for %%i in (h e l l o o o o o o o o o o o o o o) do (
       set /p "=%%i"<nul
       cscript /nologo /e:JScript "%~f0"
    )
    echo;
    
    goto :EOF
    rem // end batch portion
    
    @end
    // begin JScript chimera
    WSH.Sleep(Math.random() * 250 + 100);
    

    Note on the choice of IP to ping: For the -w switch to work as intended, the IP you ping must result in "Request timed out". You can use a non-existent LAN IP such as a 10.x.x.x or 192.168.x.x. But for widespread deployment, if you can't be certain that those ranges are unused, a link local IP in the range of 169.254 should work just fine for this purpose. Please do not use an IP in historical bogon space like 1.1.1.1 or 1.2.3.4. Just because such addresses don't reply doesn't mean your packets aren't adding to network congestion somewhere.

    Eventually as IPv4 addresses draw ever nearer to complete exhaustion, people need to be more conscientious of polluting the Internet with bogus traffic. It could be that 1.1.1.1 and 1.2.3.4 will never be useful to anyone because they are so often abused by casual scripters. But that's no reason to add to the mistreatment of those addresses. See this page for further reading, and please, save the bogons.


    Jack.bat

    Just to see how far I could take the typewriter effect, I wrote a script that outputs text similar to the X screensaver "Jack". It outputs the same line over and over, and randomly introduces typographical errors. Run it and you'll be mesmerized, rooting for the script to complete a line without any typos.

    @if (@CodeSection == @Batch) @then
    
    @echo off
    setlocal
    
    cls
    color 70
    
    call :split chars "All work and no play makes Jack a dull boy."
    
    :begin
    for %%i in (%chars%) do call :type "%%~i"
    echo;
    goto begin
    
    :split <var_to_set> <str>
    setlocal enabledelayedexpansion
    set "line="
    set "str=%~2"
    for /L %%I in (0,1,43) do set line=!line! "!str:~%%I,1!"
    endlocal & set %~1=%line%
    goto :EOF
    
    :type <char>
    cscript /nologo /e:JScript "%~f0" "%~1"
    goto :EOF
    
    @end
    // end batch / begin JScript chimera
    function pause() { WSH.Sleep(Math.random() * 250 + 100); }
    function odds(num) { return !(Math.round(Math.random() * num) % num) }
    function backspace() { WSH.StdOut.Write(String.fromCharCode(8)); }
    
    pause();
    
    if (odds(15)) {
        WSH.StdOut.Write(String.fromCharCode(Math.round(Math.random() * 95 + 32)));
        pause();
        if (!odds(20)) {
            backspace();
            pause();
        }
    }
    
    if (odds(300)) WSH.Echo('');
    if (!odds(400)) WSH.StdOut.Write(WSH.Arguments(0));
    
    0 讨论(0)
提交回复
热议问题