2014-12-02 3 views
1

Вот мой код, я не хочу, чтобы пользователь нажимал enter, чтобы сделать выбор. Я попробовал пару вещей, но ничего не попробовал.Получить пользовательский ввод без нажатия ввода

@echo off 


:debut mon menu a moi 
cls 
echo 1.Calculatrice 
echo 2.Task manager 
echo 3.msconfig 
echo 4.Notepad      
echo 5.ipconfig 
echo 6.quit 

echo. 
set /p /c choix=" Choisir votre application : " 

if "%choix%"=="1" goto menu1 
if "%choix%"=="2" goto menu2 
if "%choix%"=="3" goto menu3 
if "%choix%"=="4" goto menu4 
if "%choix%"=="5" goto menu5 
if "%choix%"=="6" goto menu6 

я не включил в меню, так как это не имеет значения

+0

Возможно, это что-то вроде этого [http://stackoverflow.com/questions/1816298/how-to-set-read-command-to-execute-without-having-to- нажмите-enter-button-in-shell) – Boss

+1

Я думаю, что вам нужна команда [choice] (http://technet.microsoft.com/en-us/library/cc732504.aspx), а не 'set/p' .. , –

ответ

1
@echo off 
    setlocal enableextensions disabledelayedexpansion 

rem OPTION 1 - Use choice command (if available) 
    echo ---------------------------------------------------  
    rem Ask for the options 
    choice /n /c 1234560 /m "Choisir votre application : " 

    rem Each option sets a errorlevel in the same order they are included. 
    rem It is necessary to test in decreasing order 
    if errorlevel 7 (
     echo selected 0 
    ) else if errorlevel 6 (
     echo selected 6 
    ) else if errorlevel 5 (
     echo selected 5 
    ) else if errorlevel 4 (
     echo selected 4 
    ) else if errorlevel 3 (
     echo selected 3 
    ) else if errorlevel 2 (
     echo selected 2 
    ) else if errorlevel 1 (
     echo selected 1 
    ) 

    rem Alternatively, this sintax can be used 
    rem In this case the order in the tests is not relevant 
    if "%errorlevel%"=="1" echo selected 1 
    if "%errorlevel%"=="2" echo selected 2 
    if "%errorlevel%"=="3" echo selected 3 
    if "%errorlevel%"=="4" echo selected 4 
    if "%errorlevel%"=="5" echo selected 5 
    if "%errorlevel%"=="6" echo selected 6 
    if "%errorlevel%"=="7" echo selected 0 


rem OPTION 2 - Use xcopy to retrieve the key press  
    echo ---------------------------------------------------  
    rem Show the prompt 
    <nul set /p ".=Choisir votre application : " 

    rem Retrieve the user input 
    set "choix=" & for /f "delims=" %%a in ('xcopy /l /w "%~f0" "%~f0" 2^>nul') do if not defined choix set "choix=%%a" 
    set "choix=%choix:~-1%" 

    rem Echo user input 
    echo %choix% 
2
@echo off 

cls 
echo 1.Calculatrice 
echo 2.Task manager 
echo 3.msconfig 
echo 4.Notepad      
echo 5.ipconfig 
echo 6.quit 

echo. 
choice /C 123456 /M "Choisir votre application : " 
goto menu%errorlevel% 


:menu1 
echo Calculatrice 

. . . 

:menu6 
:menu0 
echo Quit 

Команда choice возвращает значение Errorlevel от 1 до 6 (или 0, если пользователь нажмет Ctrl-C) , так что goto menu%errorlevel% передайте команду direclty на нужную метку без дополнительной проверки. Для получения более подробной информации, введите: choice /?

Смежные вопросы