Page 1 of 3
set/p with time?
Posted: 04 Sep 2014 08:29
by tcpman
hi all
i am creating a game and i was wondring
is it possible to use a timer with set/p
so if the timer equal to 0
and no input has been giving we go to a label or something?
i think its inpossible because of batch interpreter runs the line one by one!
but just wanted to ask!
Re: set/p with time?
Posted: 04 Sep 2014 08:33
by ShadowThief
If you're using Vista/7/8 and don't need XP support, CHOICE has a timer option you can set.
Re: set/p with time?
Posted: 04 Sep 2014 11:05
by tcpman
can you post a code exmaple plz?
the problem is choice command has some problems for me!
each time i run a program using this command i have to press shift + alt
if i dont do this is only give me beep sound!
(keybord language is english but i dont know why i have to press the shift+alt)
so is better if that can be done with set/p command
Re: set/p with time?
Posted: 04 Sep 2014 11:46
by Compo
Are you aware that you will get a beep sound every time an incorrect response is input?
Re: set/p with time?
Posted: 04 Sep 2014 11:52
by Squashman
Or did you want something like this.
Code: Select all
H:\>timeout /T 10
Waiting for 10 seconds, press a key to continue ...
Re: set/p with time?
Posted: 04 Sep 2014 12:24
by tcpman
@compo
yes i know
look for exmaple we have a choice command with wasd and i run the script now even when my language is english when i press w or s or d or a i get a beep sound but when i press shift+alt and then press one of the wasd there is no problem and its work correctly!
@squashman
look i want to get a input form user but with time!
so if user for exmaple did not enter anything as a input!
and the timer was equal to zero
then for exmaple script goes to label1
Re: set/p with time?
Posted: 04 Sep 2014 12:44
by Squashman
Then you need to use the CHOICE command.
Probably something with your code page.
Type CHCP at the command prompt and let us know what it is.
Re: set/p with time?
Posted: 04 Sep 2014 13:16
by tcpman
720
what is chcp?
sory my intenet speed is very low these days(more like dial up!) i cant serach!
Re: set/p with time?
Posted: 04 Sep 2014 13:18
by Squashman
Re: set/p with time?
Posted: 04 Sep 2014 13:25
by Aacini
You can't do that with native Batch commands. You need a third party program that allows you the get a keystroke and process it at same time that a clock is keep running. I posted such routine some time ago; this is a copy of that post:
Aacini wrote:... the subroutine below perform a timed read line, that is, it limits the time allowed to complete the input to a given number of seconds; if that time is exceeded, "Input timeout" is assigned to the input variable:
Code: Select all
@echo off
set Bell=7
set BackSpace=8
set Enter=13
set Space=32
:ReadLineTime var= seconds ["prompt"]
set %1=
set seconds=%2
Show %3 seconds ": "
set lastTime=%time:~0,-3%
:nextKey
GetKey /N
set key=%errorlevel%
if %key% equ 0 (
if %lastTime% equ %time:~0,-3% (
goto nextKey
) else (
set lastTime=%time:~0,-3%
set /A seconds-=1
if !seconds! gtr 0 (
Show 13 %3 seconds ": " %1 " " %BackSpace%
goto nextKey
) else (
StrLen %1
Show 13 %3 "0: " %Space%*!errorlevel! 13 10
set %1=Input timeout
exit /B 1
)
)
)
if %key% geq %Space% (
rem Ascii Character
Show %key%
for /F "delims=" %%a in ('Show %key%') do set "%1=!%1!%%a"
) else if %key% equ %BackSpace% (
if defined %1 (
Show %BackSpace% %Space% %BackSpace%
set "%1=!%1:~0,-1!"
) else (
Show %Bell%
)
) else if %key% equ %Enter% echo/& exit /B
goto nextKey
Previous subroutine requires Show.exe and GetKey.exe auxiliary programs; you may download they from
this site, besides a description of previous subroutine.
Antonio
Re: set/p with time?
Posted: 04 Sep 2014 13:40
by Squashman
Aacini wrote:You can't do that with native Batch commands.
I don't see why not. He is just asking to input W,A,S,D. If on of those keys isn't pressed within a certain amount of time it defaults to one of the keys. Then you check the error level to branch to what you want to do. I think that is all they are asking for. The choice command does this just fine.
Re: set/p with time?
Posted: 04 Sep 2014 13:54
by tcpman
thanks!
i will try that aacini
@squashman
yes i want to do that! if i can redirect if no input givven even better but that will do it too!
i changed the chcp to 431(united states) still same problem i need to press shift + alt
its no problem for me pressing it but because i want to give it to other pepole....
Re: set/p with time?
Posted: 04 Sep 2014 14:14
by Aacini
Squashman wrote:Aacini wrote:You can't do that with native Batch commands.
I don't see why not. He is just asking to input W,A,S,D. If on of those keys isn't pressed within a certain amount of time it defaults to one of the keys. Then you check the error level to branch to what you want to do. I think that is all they are asking for. The choice command does this just fine.
The title of this topic is "set/p with time?" In the first post, the OP said: "is it possible to use a timer with set/p" and later, he adds: "the problem is choice command has some problems for" him, "for exmaple we have a choice command with wasd..." and he must press shift+alt, "so is better if that can be done with set/p command".
Perhaps I misunderstood something?
Antonio
Re: set/p with time?
Posted: 04 Sep 2014 15:47
by ShadowThief
Alt+Shift changes the keyboard language. It sounds like the command prompt doesn't like whatever characters it thinks it's reading in.
Re: set/p with time?
Posted: 04 Sep 2014 15:49
by ShadowThief
Also, change your code page to 437 and see what happens.