2017-12-19 21 views
0

목표 : 나는 하나의 문자 (모든 문자)를 입력하고 "enter"키를 추가로 누르지 않아도 즉시 응답합니다. 사용자가 이전에 입력 한 문자없이 "enter"키를 입력하면 스크립트가 응답하지 않습니다.보이는 단일 문자를 입력 한 다음 즉시 배치로 처리하는 방법 CMD

는이 코드에 붙어입니다 :

나는 모든 기호를 수집하기 위해 조합을 시도
@Echo Off 

SET Alnum=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 

:: ordinary symbol string would not work! 
:: it must be escaped, but i don't know how. 
SET Symbol= 

SETLOCAL EnableExtensions EnableDelayedExpansion 

CALL :CharInput "Type one char: " 
ECHO Your input was: "!RetVal!" 
ECHO(

CALL :CharInput "one more char: " 
ECHO Your input was: "!RetVal!" 
ECHO(

CALL :CharInput "still need more: " 
ECHO Your input was: "!RetVal!" 
ECHO(

GOTO :eof 


:: @param1 %~1  message to print 
:: @return RetVal the single character entered by user 
:CharInput 
    IF "" == "!VisibleChars!" (
     CALL :CharacterizedWord "%Alnum%%Symbol%" 
     SET VisibleChars=!RetVal! 
    ) 
    SET /P "=%~1" < Nul 

    SET Found=false 

    FOR /F "skip=1" %%# IN ('REPLACE "%~f0" . /U /W') DO SET "CHR=%%#" 
    SET /P "=!CHR!" <Nul 

    FOR %%a in (!VisibleChars!) DO (
     IF "%%a" == "!CHR!" (
      SET Found=true 
     ) 
    ) 

    :: needs work around to make the script print only 1 message 
    :: each time the user presses the "enter" key 
    :: without any visible characters previously entered 
    IF "false" == "!Found!" (
     ECHO(
     GOTO :CharInput 
    ) ELSE (
     SET "RetVal=!CHR!" 
     ECHO(
    ) 
GOTO :eof 


:: @param1 %~1  the word being characterized 
:: @return RetVal the string contains words each of single character 
:CharacterizedWord 
    SET Word=%~1 
    SET tempstr=%~1 
    SET count=0 
    :CharacterizedWordLoop 
    IF DEFINED tempstr (
     SET tempstr=%tempstr:~1% 
     SET /a count+=1 
     SET /a pos=%count%-1 
     SET RetVal=!RetVal! !Word:~%pos%,1! 
     GOTO :CharacterizedWordLoop 
    ) 
GOTO :eof 
  1. 가 하나의 단어로가 문자. "Symbol"var의 공허함을 채울 수있는 사람이 있습니까?

  2. 새로운 문자를 찾을 때마다 목록을 편집하지 않고 보이는 문자 목록을 확장하는 다른 알고리즘이 있습니까?

  3. 이 앱은 사용자가 "enter"키를 누를 때마다 여전히 "유형 1 문자 :"메시지를 인쇄합니다. 누구든지이 문제를 해결할 수 있습니까?

+2

시험해주세요. https://stackoverflow.com/questions/13166100/how-can-i-return-a-single-character-from-the-console-in-a-batch-script-without-p CHOICE – Ctznkane525

+0

@ 존 케인 : 상징은 어때요? 네이티브 선택은 기호를 지원하지 않습니다. –

+3

에는 선택시 특수 문자를 사용하는 방법에 대한 정보가 있습니다. 기술적으로 지원되지 않지만 해결 방법은 https://stackoverflow.com/questions/41456194/how-to-use-special-characters-in-choice-command-batch-file – Ctznkane525

답변

0

콘솔에서 $ 호스트 ReadKey 메서드를 사용하면 Enter 키가 필요없는 단일 키를 읽을 수 있습니다. 이 일이 당신이 원하는대로합니까?

"thekey.ps1"이라는 파일에 넣습니다.

<# Ignore: 
    Enter 
    Shift 
    Ctrl 
    Alt 18 
    CapsLock 
    LeftArrow 
    RightArrow 
    UpArrow 
    DownArrow 
    Delete 
    End 35 
    Home 36 
    Insert 45 
    PgUp 33 
    PgDown 34 
    Windows 91 
#> 

$nowatch = $(13, 16, 17, 18, 20, 37, 38, 39, 40, 46, 35, 36, 45, 33, 34, 91) 

do { 
    $key = $host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown') 

    #$key.VirtualKeyCode 

    $vkc = $key.VirtualKeyCode 
    $k = $key.Character 
} while ($nowatch -contains $vkc) 
$k 

당신이 발견 #$key.VirtualKeyCode에서 주석 '#'를 제거하고 실행합니다. \ thekey.ps1 PowerShell은 콘솔 (안 ISE)에해야 할 다른 키가있는 경우.

"thekey.bat"라는 파일에 넣습니다. 또한

@ECHO OFF 
:Head 
FOR /F "usebackq tokens=*" %%k IN (`powershell -NoProfile -File .\thekey.ps1`) DO (
    SET "THEKEY=%%k" 
) 
ECHO THEKEY is %THEKEY% 
GOTO Head 

:

당신이 당신이하지 않는 것보다 인식 할 키를 지정하는 것이 더 쉬울 수 있습니다.

$keylist = @() 
0x21..0x7E | ForEach-Object { $keylist += $_ } 

do { 
    $key = $host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown') 

    #$key.VirtualKeyCode 

    $vkc = $key.VirtualKeyCode 
    $k = $key.Character 
} while ($keylist -notcontains $vkc) 
$k 
+0

스크립트가 "sift"키를 눌렀을 때 바로 응답합니다. "THEKEY is"가 컴패니언 키 앞에 여러 번 인쇄됩니다. 이것은 내가 원하는 행동이 아닙니다. –

+0

@thethethemetalmetal - 코드를 수정 한 내용을 확인하십시오. 이것이 당신이 찾고있는 것에 더 가깝기를 바랍니다. – lit