This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,2 @@
Flush the [[input device::keyboard]] buffer. This reads characters from the keyboard input and discards them until there are no more currently buffered, and then allows the program to continue. ''The program must not wait for users to type anything.''
[[user input::task| ]]

View file

@ -0,0 +1,2 @@
---
note: Keyboard input

View file

@ -0,0 +1,20 @@
with Ada.Text_IO;
procedure Flushtest is
use Text_IO;
begin
Put_Line ("Type anything for 2 s");
delay 2.0;
Flush_Input:
declare
Ch : Character;
More : Boolean;
begin
loop
Get_Immediate (Ch, More);
exit when not More;
end loop;
end Flush_Input;
New_Line;
Put_Line ("Okay, thanks. Here is some input from you:");
Put_Line (Get_Line);
end Flushtest;

View file

@ -0,0 +1 @@
10 IF INKEY$ <> "" THEN GO TO 10

View file

@ -0,0 +1,68 @@
#include <stdio.h>
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>
void set_mode(int want_key)
{
static struct termios old, new;
if (!want_key) {
tcsetattr(STDIN_FILENO, TCSANOW, &old);
return;
}
tcgetattr(STDIN_FILENO, &old);
new = old;
new.c_lflag &= ~(ICANON);
tcsetattr(STDIN_FILENO, TCSANOW, &new);
}
int get_key()
{
int c = 0;
fd_set fs;
FD_ZERO(&fs);
FD_SET(STDIN_FILENO, &fs);
select(STDIN_FILENO + 1, &fs, 0, 0, 0);
if (FD_ISSET(STDIN_FILENO, &fs)) {
c = getchar();
set_mode(0);
}
return c;
}
int main()
{
int c = 0;
while (c != 'n') {
set_mode(1);
/* flush pending input so we won't format the hardrive
because user accidentally typed 'y' before we even prompted */
tcflush(STDIN_FILENO, TCIFLUSH);
printf("Show this prompt again [Yes/No/Ignore you]? ");
fflush(stdout);
switch(c = tolower(get_key())) {
case 'y': putchar('\n');
break;
case 'n': printf("\nDone\n");
break;
case 'i': puts("\nI'll ignore keys for 5 seconds");
sleep(5);
putchar('\n');
break;
default:
puts("\nAssume that was the cat.");
}
}
return 0;
}

View file

@ -0,0 +1,18 @@
extern (C) {
void _STI_conio();
void _STD_conio();
int kbhit();
int getch();
}
void main() {
void flushKB() {
while (kbhit()) getch();
}
_STI_conio();
flushKB();
_STD_conio();
}

View file

@ -0,0 +1,2 @@
while get_key()!=-1 do
end while

View file

@ -0,0 +1,3 @@
procedure flushKB()
while kbhit() do getch() # flush input
end

View file

@ -0,0 +1,17 @@
use Term::ReadKey;
ReadMode 'restore'; # Flush the keyboard and returns input stream to initial state
# ReadMode 0; # Numerical equivalent of keyboard restore (move comment marker to use instead)
# A more complete example for use in keyboard handler programming.
# We should also check we are being used in an interactive context (not done here).
use Term::ReadKey;
ReadMode 'cbreak';
# Flush the keyboard in terminal character break mode
while (defined ReadKey -1) {
# Do nothing
}
# Don't forget to restore the readmode, when we are finished using the keyboard
ReadMode 'restore';

View file

@ -0,0 +1,8 @@
def flush_input():
try:
import msvcrt
while msvcrt.kbhit():
msvcrt.getch()
except ImportError:
import sys, termios
termios.tcflush(sys.stdin, termios.TCIOFLUSH)

View file

@ -0,0 +1,2 @@
require 'io/console'
$stdin.iflush

View file

@ -0,0 +1 @@
loop { $stdin.read_nonblock(256) } rescue nil

View file

@ -0,0 +1,64 @@
class IO
def discard_input
icanon = false
if tty?
begin
# With Ruby 1.9.3, simply call IO#iflush.
require 'io/console'
return iflush
rescue LoadError
# Try to run stty(1) to check if this terminal uses
# canonical input mode. Acts like `stty -a`, but redirects
# stdin from tty. Works with Ruby 1.8, no Process#spawn.
r, w, pid = nil
begin
r, w = IO.pipe
pid = fork do
IO.for_fd(0).reopen(self) # stdin from tty
IO.for_fd(1).reopen(w) # stdout to pipe
exec 'stty', '-a'
end
w.close; w = nil
icanon = (not r.read.include? "-icanon")
rescue
# stty(1) only works with Unix clones.
ensure
pid and Process.wait pid
w and w.close
r and r.close
end
end
end
if icanon
# Turn off canonical input mode.
pid = nil
begin
pid = fork do
IO.for_fd(0).reopen(self) # stdin from tty
exec 'stty', '-icanon'
end
ensure
pid and Process.wait pid
end
end
# Discard input.
loop { $stdin.read_nonblock(256) } rescue nil
if icanon
# Turn on canonical input mode.
pid = nil
begin
pid = fork do
IO.for_fd(0).reopen(self) # stdin from tty
exec 'stty', 'icanon'
end
ensure
pid and Process.wait pid
end
end
nil
end
end

View file

@ -0,0 +1,9 @@
# Demonstration: discard input, then input a line from user.
puts 'Type anything for 2 seconds.'
sleep 2
$stdin.discard_input
print 'Enter a line? '
if line = $stdin.gets
then print 'Got line. ', line
else puts 'No line!'
end

View file

@ -0,0 +1,7 @@
# No waiting for input
fconfigure stdin -blocking 0
# Drain the data by not saving it anywhere
read stdin
# Flip back into blocking mode (if necessary)
fconfigure stdin -blocking 1