Keyboard input/Obtain a Y or N response: Difference between revisions

m
Use inputReady instead of the deprecated function keypress
No edit summary
m (Use inputReady instead of the deprecated function keypress)
 
(6 intermediate revisions by 4 users not shown)
Line 356:
70 PRINT "Try again"
80 GOTO 30</syntaxhighlight>
 
==={{header|True BASIC}}===
<syntaxhighlight lang="qbasic">LIBRARY "DefLib.trc"
 
DECLARE DEF INKEY$
PRINT "Press Y or N to continue."
DO
LET kbd$ = ""
DO WHILE kbd$ = ""
LET kbd$ = UCASE$(INKEY$)
LOOP
IF kbd$ <> "Y" AND kbd$ <> "N" THEN SOUND 800, .25
LOOP UNTIL kbd$ = "Y" OR kbd$ = "N"
PRINT "The response was "; kbd$
END</syntaxhighlight>
 
==={{header|Yabasic}}===
Line 1,091 ⟶ 1,106:
return ch == 'y' or 'Y';
]; -).</syntaxhighlight>
 
=={{header|Java}}==
The task specification that there should be no need for the user to press the enter key,
creates an awkward situation for the Java language.
 
However, a short program that waits for the user to press return can easily be constructed.
<syntaxhighlight lang="java">
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.awt.EventQueue;
import java.awt.event.KeyAdapter;
import javax.swing.JFrame;
 
public final class KeyboardInputObtainYOrN {
 
public static void main(String[] aArgs) {
EventQueue.invokeLater( () -> { new Test("Obtain Y or N"); } );
}
}
 
final class Test extends JFrame {
public Test(String aTitle) {
super(aTitle);
addKeyListener( new YesOrNoKeyAdapter() );
setVisible(true);
try {
while ( System.in.available() > 0 ) {
System.in.read();
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
System.out.println("Do you want to quit the program? Y / N");
}
}
 
final class YesOrNoKeyAdapter extends KeyAdapter {
@Override
public void keyPressed(KeyEvent aKeyEvent) {
if ( aKeyEvent.getKeyCode() == KeyEvent.VK_Y ) {
System.out.println("Y was pressed, quitting the program");
Runtime.getRuntime().exit(0);
} else if ( aKeyEvent.getKeyCode() == KeyEvent.VK_N ) {
System.out.println("N was pressed, but the program is about to end anyway");
Runtime.getRuntime().exit(0);
} else {
System.out.println("Please try again, only Y or N are acceptable");
}
}
}
</syntaxhighlight>
 
=={{header|JavaScript}}==
Line 1,872 ⟶ 1,945:
end
</syntaxhighlight>
 
=={{header|RPL}}==
≪ "Yes or No?" 1 DISP
'''DO'''
'''DO UNTIL''' KEY '''END'''
'''UNTIL''' "YN" SWAP POS '''END'''
"YN" LAST DUP SUB CLMF
´'''TASK'''’ STO
 
=={{header|Ruby}}==
Line 1,968 ⟶ 2,050:
var char: answer is ' ';
begin
while keypressedinputReady(KEYBOARD) do
ignore(getc(KEYBOARD));
end while;
Line 2,252 ⟶ 2,334:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">import "io" for Stdin, Stdout
 
Stdin.isRaw = true // input is neither echoed nor buffered in this mode
29

edits