使用xcopy实现choice.exe获得按键输入,支持组合键

先看代码:

@echo off

:get_key
  set "key="
  for /f "delims=" %%a in ('xcopy /w "%~f0" "%~f0" 2^>nul') do if not defined key set "key=%%a"
  set "key=%key:~-1%"
  echo %key%
 
pause

===================引用自===================
一个批处理版本的2048游戏:https://rosettacode.org/wiki/2048#Batch_File

-----------------------------------------------------------------------------------------

 这个方式好象能支持比 choice 更多的字符, 比如 , . / Tab 字符, 甚至组合键 Ctrl+A   Ctrl+Q 等
另外, 我把参数稍改了下, 在 win7 64 位仍能行

  xcopy /w . . 2^>NUL
  或者
  xcopy /w \ \ 2^>NULCOPY

我能找到的最早出处
February 07, 2013, 03:07:23 AM
jeb @ www.computerhope.com
http://www.computerhope.com/forum/index.php/topic,135713.msg872140.html?PHPSESSID=70a491e69021f3e926539660fee754b9#msg872140
随后, jeb 又在 stackoverflow 和 groups.google.com/forum 都发表过此代码应用例
http://stackoverflow.com/questio ... r/15160168#15160168
https://groups.google.com/forum/ ... atch.nt/hpNOPhNfwtI
我找到的时间最早的示例代码:

@echo off
setlocal EnableDelayedExpansion
if "%~1"==":::" goto :spinnerThread
 
:menuLoop
<nul set /p menu=Select menu[1 or 2]=
call :GetKey
echo(
echo Pressed '!key!'
if !key!==1 call :menu1
if !key!==2 call :menu2
if !key!==2 call :menu2
goto :menuLoop
 
:menu1
:menu2
call :spinnerStart
rem do some work
ping localhost -n 3  > nl
call :spinnerStop
echo Finished
exit /b
 
:spinnerStart
del spinnerStop.tmp > nul 2>&1
start /b "" cmd /c "%~df0" :::
exit /b
 
:spinnerStop
echo dummy > spinnerStop.tmp
:__spinnerStop
if exist spinnerStop.tmp goto :__spinnerStop
exit /b
 
:spinnerThread
for /f %%a in ('copy /Z "%~f0" nul') do set "CR=%%a"
set "spinChars=\|/-"
 
:spinnerLoop
set /a "spinner=(spinner + 1) %% 4"
<nul set /p ".=Waiting...!spinChars:~%spinner%,1!!CR!"
ping localhost -n 2 > nul 2>&1
if not exist spinnerStop.tmp goto :spinnerLoop
del spinnerStop.tmp > nul 2>&1
echo(
exit /b
 
:GetKey
set "key="
for /F "usebackq delims=" %%L in (`xcopy /L /w "%~f0" "%~f0" 2^>NUL`) do (
  if not defined key set "key=%%L"
)
set "key=%key:~-1%"
exit /b

---------------------------------------------------------------------------------------

再简化:

xcopy /w . 2^>NULCOPY

话说从链接中看到了熟悉的...

for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"
For /F delims^=^ eol^= %%Z in ("!Char!") Do Set "Intro=0"
Echo(

看来歪果仁也发现了这些

 

出处:http://www.bathome.net/thread-41643-1-1.html

posted on 2023-11-08 16:27  jack_Meng  阅读(22)  评论(0编辑  收藏  举报

导航