WSH JScript single-character input ?

  • Thread starter Dr J R Stockton
  • Start date
D

Dr J R Stockton

Can command-line JScript, running in Windows script Host, read a
*single* character from the keyboard without needing Enter? My old
16-bit Pascal directory program HUNT accepts single-character responses
for Yes/No/Quit, and it would be nice to have my 32-bit WSH version of
it do the same.

If anyone wants to look, the WSH is SEAKFYLE.JS ("documentation" is
SEAKFYLE.HTM), which is called by SEEK.BAT, all linked from
<http://www.merlyn.demon.co.uk/programs/32-bit/00index.htm>.
 
E

Evertjan.

Dr J R Stockton wrote on 04 dec 2011 in microsoft.public.scripting.jscript:
Can command-line JScript, running in Windows script Host, read a
*single* character from the keyboard without needing Enter? My old
16-bit Pascal directory program HUNT accepts single-character responses
for Yes/No/Quit, and it would be nice to have my 32-bit WSH version of
it do the same.

StIn without pipe will read from the keyboard.

========== test.js ===========
var input = '';
while (!WScript.StdIn.AtEndOfLine) {
input += WScript.StdIn.Read(1) + '-';
}
WScript.Echo(input);
==============================

run with cscript [wscript has no StIn] in the cmd-console:

cscript test.js
 
D

Dr J R Stockton

Dr J R Stockton wrote on 04 dec 2011 in microsoft.public.scripting.jscript:
Can command-line JScript, running in Windows script Host, read a
*single* character from the keyboard without needing Enter?  My old
16-bit Pascal directory program HUNT accepts single-character responses
for Yes/No/Quit, and it would be nice to have my 32-bit WSH version of
it do the same.

StIn without pipe will read from the keyboard.

========== test.js ===========
var input = '';
while (!WScript.StdIn.AtEndOfLine) {
   input += WScript.StdIn.Read(1) + '-';}

WScript.Echo(input);
==============================

run with cscript [wscript has no StIn] in the cmd-console:

cscript test.js


Requires Enter.
 
E

Evertjan.

Dr J R Stockton wrote on 05 dec 2011 in comp.lang.javascript:
Dr J R Stockton wrote on 04 dec 2011 in
microsoft.public.scripting.jscrip t:
Can command-line JScript, running in Windows script Host, read a
*single* character from the keyboard without needing Enter? ÿMy old
16-bit Pascal directory program HUNT accepts single-character
responses for Yes/No/Quit, and it would be nice to have my 32-bit
WSH version of it do the same.

StIn without pipe will read from the keyboard.

========== test.js ==========
var input = '';
while (!WScript.StdIn.AtEndOfLine) {
ÿ ÿinput += WScript.StdIn.Read(1) + '-';}

WScript.Echo(input);
======================== =====>
run with cscript [wscript has no StIn] in the cmd-console:

cscript test.js


Requires Enter.

Then I guess it cannot be done, as shown by scriptingguy in 2004:

<http://blogs.technet.com/b/heyscriptingguy/archive/2004/10/05/how-can-i-
pause-a-script-and-then-resume-it-when-a-user-presses-a-key-on-the-
keyboard.aspx>
 
T

Tom Lavedas

Dr J R Stockton wrote on 05 dec 2011 in comp.lang.javascript:




Dr J R Stockton wrote on 04 dec 2011 in
microsoft.public.scripting.jscrip t:
Can command-line JScript, running in Windows script Host, read a
*single* character from the keyboard without needing Enter? My old
16-bit Pascal directory program HUNT accepts single-character
responses for Yes/No/Quit, and it would be nice to have my 32-bit
WSH version of it do the same.
StIn without pipe will read from the keyboard.
========== test.js ==========
var input = '';
while (!WScript.StdIn.AtEndOfLine) {
input += WScript.StdIn.Read(1) + '-';}
WScript.Echo(input);
======================== =====>
run with cscript [wscript has no StIn] in the cmd-console:
cscript test.js
Requires Enter.

Then I guess it cannot be done, as shown by scriptingguy in 2004:

<http://blogs.technet.com/b/heyscriptingguy/archive/2004/10/05/how-can-i-
pause-a-script-and-then-resume-it-when-a-user-presses-a-key-on-the-
keyboard.aspx>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)- Hide quoted text -

- Show quoted text -

I wrote a JScript routine a little while back that used Sendkeys to
implement a one-letter routine that didn't need an enter key and
supported a timeout. Unfortunately, Win7 disables Sendkeys in a
command window, so it's now broken. On the other hand, Win7 added
Choice.exe back to the utility list. It supports single keystroke
entry.

I also wrote this little batch kludge that receives single key strokes
Y/N and control-C for cancel ...

:: YN.cmd - Yes/No/Cancelled test
@echo off
setlocal>%temp%.\1
SET /P =Your answer (Y/N)?<nul
for /f "tokens=2 delims=?" %%a in (
'xcopy %temp%.\1 %temp%.\1 /p /y ^< con 2^>NUL ^|find "?"'
) do set "Answer=%%a"
del %temp%.\1
ECHO.%Answer% % optional %
if "%Answer%"==" " ECHO Cancelled % optional % & exit /b 255
echo %Answer%| find /i "y" >NUL
if errorlevel 1 ECHO No % optional % & exit /b 1
echo Yes % optional %

It is not case sensitive. It returns three Errorlevels: Y = 0, N = 1,
Cancel = 255.
______________________________
Tom Lavedas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top