set/p with time?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
tcpman
Posts: 53
Joined: 05 Mar 2014 15:01

set/p with time?

#1 Post by tcpman » 04 Sep 2014 08:29

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!

ShadowThief
Expert
Posts: 1167
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: set/p with time?

#2 Post by ShadowThief » 04 Sep 2014 08:33

If you're using Vista/7/8 and don't need XP support, CHOICE has a timer option you can set.

tcpman
Posts: 53
Joined: 05 Mar 2014 15:01

Re: set/p with time?

#3 Post by tcpman » 04 Sep 2014 11:05

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

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: set/p with time?

#4 Post by Compo » 04 Sep 2014 11:46

Are you aware that you will get a beep sound every time an incorrect response is input?

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: set/p with time?

#5 Post by Squashman » 04 Sep 2014 11:52

Or did you want something like this.

Code: Select all

H:\>timeout /T 10

Waiting for  10 seconds, press a key to continue ...

tcpman
Posts: 53
Joined: 05 Mar 2014 15:01

Re: set/p with time?

#6 Post by tcpman » 04 Sep 2014 12:24

@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

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: set/p with time?

#7 Post by Squashman » 04 Sep 2014 12:44

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.

tcpman
Posts: 53
Joined: 05 Mar 2014 15:01

Re: set/p with time?

#8 Post by tcpman » 04 Sep 2014 13:16

720

what is chcp?
sory my intenet speed is very low these days(more like dial up!) i cant serach!

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: set/p with time?

#9 Post by Squashman » 04 Sep 2014 13:18


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

Re: set/p with time?

#10 Post by Aacini » 04 Sep 2014 13:25

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

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: set/p with time?

#11 Post by Squashman » 04 Sep 2014 13:40

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.

tcpman
Posts: 53
Joined: 05 Mar 2014 15:01

Re: set/p with time?

#12 Post by tcpman » 04 Sep 2014 13:54

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....

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

Re: set/p with time?

#13 Post by Aacini » 04 Sep 2014 14:14

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

ShadowThief
Expert
Posts: 1167
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: set/p with time?

#14 Post by ShadowThief » 04 Sep 2014 15:47

Alt+Shift changes the keyboard language. It sounds like the command prompt doesn't like whatever characters it thinks it's reading in.

ShadowThief
Expert
Posts: 1167
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: set/p with time?

#15 Post by ShadowThief » 04 Sep 2014 15:49

Also, change your code page to 437 and see what happens.

Post Reply