User input delay?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Boombox
Posts: 80
Joined: 18 Oct 2012 05:51

User input delay?

#1 Post by Boombox » 06 Nov 2012 09:25

.
Hello again CMD folk.

How do I request a key press, but only for a limited amount of time? e.g

Press any key now to boot from CD-ROM...

Cheers.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: User input delay?

#2 Post by foxidrive » 06 Nov 2012 10:05

Use the CHOICE command. Native to various versions of Windows and choice clones downloadable for OS that don't have it.


CHOICE version 4.1, Copyright (C) 1994--2003 Jim Hall, jhall@freedos.org
Waits for the user to press a key, from a list of choices
Usage: CHOICE [ /B ] [ /C[:]choices ] [ /N ] [ /S ] [ /T[:]c,nn ] [ text ]
/B - Sound an alert (beep) at prompt
/C[:]choices - Specifies allowable keys. Default is: yn
/N - Do not display the choices at end of prompt
/S - Be case-sensitive
/T[:]c,nn - Automatically choose key c after nn seconds
text - The text to display as a prompt

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: User input delay?

#3 Post by Aacini » 06 Nov 2012 10:55

You may use my GetKey auxiliary program to do that; search for GetKey.exe.hex in this post.

Code: Select all

:GetKeyTime seconds
set seconds=%1
set lastTime=%time:~0,-3%
:wait
   GetKey /N
   if %errorlevel% neq 0 set seconds=0
   if %lastTime% neq %time:~0,-3% (
      set lastTime=%time:~0,-3%
      set /A seconds-=1
   )
if %seconds% gtr 0 goto wait
exit /B
If you just want to wait for a key, then CHOICE command is a better option.

However, this method allows you to execute other things while the program waits. For example ReadLineTime subroutine (in above mentioned post) allows to limit the time to read a complete line of text and show the remaining time in the prompt string.

Antonio

Boombox
Posts: 80
Joined: 18 Oct 2012 05:51

Re: User input delay?

#4 Post by Boombox » 06 Nov 2012 14:04

.
Thanks Antonio. That's some cool stuff!

And thank you Foxi, as always.

Post Reply