Page 1 of 1
Experimenting on User Input....
Posted: 29 Aug 2015 07:20
by Meerkat
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):
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:
EDIT:It also does not store the char to the "key" variable.
Is there a way to not display the key pressed? Thanks everyone!!!
Meerkat
Re: Experimenting on User Input....
Posted: 30 Aug 2015 19:26
by dbenham
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:
Dave Benham
Re: Experimenting on User Input....
Posted: 30 Aug 2015 23:14
by Meerkat
So there is really no way to redirect CON to Stdout/StdErr?
Meerkat
Re: Experimenting on User Input....
Posted: 31 Aug 2015 02:47
by OperatorGK
Strangely, this doesn't do anything on my Win10. What Windows version you are using?
Re: Experimenting on User Input....
Posted: 31 Aug 2015 02:52
by foxidrive
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.
Re: Experimenting on User Input....
Posted: 31 Aug 2015 03:35
by Meerkat
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
Re: Experimenting on User Input....
Posted: 31 Aug 2015 03:38
by foxidrive
OperatorGK wrote:What Windows version you are using?
FWIW I tested it on XP pro, Win 7 and Win 8.1
Re: Experimenting on User Input....
Posted: 31 Aug 2015 03:44
by Meerkat
There is really no way to catch the character being pressed from SC?
Re: Experimenting on User Input....
Posted: 31 Aug 2015 06:31
by Squashman
As far as I know, everything going to con, gets output on the console.
Re: Experimenting on User Input....
Posted: 31 Aug 2015 06:53
by dbenham
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
Re: Experimenting on User Input....
Posted: 31 Aug 2015 07:11
by Meerkat
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
Oh... Sorry
BTW, Thanks everyone!
Meerkat
Re: Experimenting on User Input....
Posted: 31 Aug 2015 09:53
by OperatorGK
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.
Re: Experimenting on User Input....
Posted: 31 Aug 2015 16:44
by foxidrive
OperatorGK wrote:I meant, SC exists and works, but "for..." command simply doesn't do anything.
Did you press control c ?