Experimenting on User Input....

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Meerkat
Posts: 89
Joined: 19 Jul 2015 02:27
Location: Philippines

Experimenting on User Input....

#1 Post by Meerkat » 29 Aug 2015 07:20

Hi again!

I am trying this as user input:

Code: Select all

@echo off
for /f %%i in ('sc ^>nul') do echo %%i
pause

It has an advantage that it can handle Ctrl+C, Ctrl+Z, and some other "special" chars. It also does not echo the CRLF.
Sample Output (Pressing Ctrl+C):

Code: Select all

♥Press any key to continue . . .


Now, I will try to NOT print the char and store it instead to a variable. I tried this,

Code: Select all

@echo off
for /f %%i in ('sc ^>nul') do set "key=%%i" >nul

But when I press something (again, Ctrl+C), it still displays the char pressed!
Output:

Code: Select all



EDIT:
It also does not store the char to the "key" variable. :?

Is there a way to not display the key pressed? Thanks everyone!!! :D

Meerkat

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Experimenting on User Input....

#2 Post by dbenham » 30 Aug 2015 19:26

It appears that SC prints the character directly to the console, without going through stdout or stderr. So there is no way to capture the character :(
Note how the following still prints the pressed character to the screen:

Code: Select all

sc >nul 2>nul


Dave Benham

Meerkat
Posts: 89
Joined: 19 Jul 2015 02:27
Location: Philippines

Re: Experimenting on User Input....

#3 Post by Meerkat » 30 Aug 2015 23:14

So there is really no way to redirect CON to Stdout/StdErr?

Meerkat

OperatorGK
Posts: 66
Joined: 13 Jan 2015 06:55

Re: Experimenting on User Input....

#4 Post by OperatorGK » 31 Aug 2015 02:47

Strangely, this doesn't do anything on my Win10. What Windows version you are using?

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

Re: Experimenting on User Input....

#5 Post by foxidrive » 31 Aug 2015 02:52

OperatorGK wrote:Strangely, this doesn't do anything on my Win10. What Windows version you are using?


Could it be because the default console font doesn't have a glyph for the control c character?
I can't test it here - just raising the idea.

Meerkat
Posts: 89
Joined: 19 Jul 2015 02:27
Location: Philippines

Re: Experimenting on User Input....

#6 Post by Meerkat » 31 Aug 2015 03:35

OperatorGK wrote:Strangely, this doesn't do anything on my Win10. What Windows version you are using?


Just type SC in the console first if it exists.

Meerkat

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

Re: Experimenting on User Input....

#7 Post by foxidrive » 31 Aug 2015 03:38

OperatorGK wrote:What Windows version you are using?


FWIW I tested it on XP pro, Win 7 and Win 8.1

Meerkat
Posts: 89
Joined: 19 Jul 2015 02:27
Location: Philippines

Re: Experimenting on User Input....

#8 Post by Meerkat » 31 Aug 2015 03:44

There is really no way to catch the character being pressed from SC?

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

Re: Experimenting on User Input....

#9 Post by Squashman » 31 Aug 2015 06:31

As far as I know, everything going to con, gets output on the console.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Experimenting on User Input....

#10 Post by dbenham » 31 Aug 2015 06:53

Squashman wrote:As far as I know, everything going to con, gets output on the console.

Translation - No, it cannot be captured in a variable :wink:

Meerkat
Posts: 89
Joined: 19 Jul 2015 02:27
Location: Philippines

Re: Experimenting on User Input....

#11 Post by Meerkat » 31 Aug 2015 07:11

dbenham wrote:
Squashman wrote:As far as I know, everything going to con, gets output on the console.

Translation - No, it cannot be captured in a variable :wink:


Oh... Sorry :oops:

BTW, Thanks everyone!

Meerkat

OperatorGK
Posts: 66
Joined: 13 Jan 2015 06:55

Re: Experimenting on User Input....

#12 Post by OperatorGK » 31 Aug 2015 09:53

Meerkat wrote:
OperatorGK wrote:Strangely, this doesn't do anything on my Win10. What Windows version you are using?


Just type SC in the console first if it exists.

Meerkat

I meant, SC exists and works, but "for..." command simply doesn't do anything.

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

Re: Experimenting on User Input....

#13 Post by foxidrive » 31 Aug 2015 16:44

OperatorGK wrote:I meant, SC exists and works, but "for..." command simply doesn't do anything.



Did you press control c ?

Post Reply