Is there a way to set variables as an escape character using set / p?

I would like to catch escape characters like ctrl+ z ctrl+ + aand so on. When we press this escape combination, the prompt is displayed ^Z ^A, but when I try to use them as a value for a variable, the code does not work.

set /p "input=> "
echo/%input%

The above code shows as output only %input%when I put an escape character, the variable is empty. So, is there a way to catch them? Or is it impossible?

+4
source share
3 answers

If you want to catch these keys, you need an external program, you can write your own input using missusing XCOPY.

, F1..F12

@echo off

:loop
set "key="
for /F "usebackq delims=" %%L in (`xcopy /L /w "%~f0" "%~f0" 2^>NUL`) do (
  if not defined key set "key=%%L"
)
setlocal EnableDelayedExpansion
set "key=!key:~-1!"
echo '!key!'
endlocal
goto :loop
+1

^A, ^B, ^G ( ^ ctrl+letter). , ^Z ^C .

, . :

C:\> Echo ^A > tmp.txt
C:\> set /p ctrl_A=<tmp.txt
C:\> Echo ^G > tmp.txt
C:\> set /p ctrl_G=<tmp.txt
C:\> del tmp.txt
C:\> Echo %ctrl_A%

C:\> Echo %ctrl_G%


C:\> Rem In above double blank you should hear a beep from your computer.

. : , ^ , ctrl+letter.

, , .

  • cmd- Echo ^G > tmp.txt
  • Notepad tmp.txt
  • , , - . ( )
  • , del tmp.txt cmd
  • ,

CMD BATCH!

, , .

+2

Your code

set /p "input=> "
echo/%input%

it seems to work if there is at least one character after the code (it can also be a space) (for example, try typing Hello^G World(using Ctrl-G to create ^G). It creates "Hello World", plus a beep.

Hello World^Gdoes not work, Hello World^G<space>does.

+1
source

All Articles