Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,7 @@
---
category:
- Hardware
- Terminal control
- Simple
from: http://rosettacode.org/wiki/Keyboard_input/Flush_the_keyboard_buffer
note: Keyboard input

View file

@ -0,0 +1,13 @@
[[user input::task| ]]
{{omit from|ACL2}}
{{omit from|GUISS}}
{{omit from|PARI/GP}}
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.''
<br><br>

View file

@ -0,0 +1,2 @@
mov ax,0C00h ;equivalent of "mov ah,0Ch mov al,0"
int 21h

View file

@ -0,0 +1,2 @@
mov ax,0C0Ah
int 21h ;flush the keyboard buffer then immediately ask the user to type in a sentence and hit Enter when done.

View file

@ -0,0 +1,367 @@
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program keyboardInput64.s */
/*******************************************/
/* Constantes file */
/*******************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeConstantesARM64.inc"
.equ IOCTL, 0x1D // Linux syscall
.equ SIGACTION, 0x86 // Linux syscall
.equ SYSPOLL, 0x16 // Linux syscall
.equ CREATPOLL, 0x14 // Linux syscall
.equ CTLPOLL, 0x15 // Linux syscall
.equ TCGETS, 0x5401
.equ TCSETS, 0x5402
.equ ICANON, 2
.equ ECHO, 10
.equ POLLIN, 1
.equ EPOLL_CTL_ADD, 1
.equ SIGINT, 2 // Issued if the user sends an interrupt signal (Ctrl + C)
.equ SIGQUIT, 3 // Issued if the user sends a quit signal (Ctrl + D)
.equ SIGTERM, 15 // Software termination signal (sent by kill by default)
.equ SIGTTOU, 22 //
.equ BUFSIZE, 80
/*******************************************/
/* Structures */
/********************************************/
/* structure termios see doc linux*/
.struct 0
term_c_iflag: // input modes
.struct term_c_iflag + 4
term_c_oflag: // output modes
.struct term_c_oflag + 4
term_c_cflag: // control modes
.struct term_c_cflag + 4
term_c_lflag: // local modes
.struct term_c_lflag + 4
term_c_cc: // special characters
.struct term_c_cc + 20 // see length if necessary
term_fin:
/* structure sigaction see doc linux */
.struct 0
sa_handler:
.struct sa_handler + 8
sa_mask:
.struct sa_mask + 8
sa_flags:
.struct sa_flags + 8
sa_sigaction:
.struct sa_sigaction + 8
sa_fin:
/* structure poll see doc linux */
.struct 0
poll_event:
.struct poll_event + 8
poll_fd: // File Descriptor
.struct poll_fd + 8
poll_fin:
/*********************************/
/* Initialized data */
/*********************************/
.data
szMessPgmOk: .asciz "End program OK.\n"
szMessErreur: .asciz "Error detected.\n"
szCarriageReturn: .asciz "\n"
szMessCodeErr: .asciz "Error code décimal : @ \n"
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
.align 4
iEnd: .skip 8 // 0 loop 1 = end loop
iTouche: .skip BUFSIZE // value key pressed
stOldtio: .skip term_fin // old terminal state
stCurtio: .skip term_fin // current terminal state
stSigAction: .skip sa_fin // area signal structure
stSigAction1: .skip sa_fin
stSigAction2: .skip sa_fin
stSigAction3: .skip sa_fin
stevents: .skip 16
sZoneConv: .skip 24
szBuffer: .skip BUFSIZE
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: // entry of program
bl initTerm // terminal init
cmp x0,0 // error ?
blt 100f
bl initPoll // epoll instance init
cmp x0,0
blt 100f
mov x22,x0 // save epfd
mov x20,0 // indice
ldr x21,qAdrszBuffer
1:
mov x0,x22 // epfd
bl waitKey
cmp x0,0
beq 1b // no ket pressed -> loop
blt 3f // error ?
bl readKey // read one key
cmp x0,3 // ctrl-C
beq 3f
cmp x0,113 // saisie q (quit) ?
beq 3f
cmp x0,81 // saisie Q (Quit)?
beq 3f
cmp x0,0xD // <enter> ?
beq 2f
strb w0,[x21,x20] // store byte in buffer
add x20,x20,1 // increment indice
b 1b // and loop
2: // display buffer
mov x0,0 // store 0 final
strb w0,[x21,x20]
mov x0,x21 // display buffer
bl affichageMess
ldr x0,qAdrszCarriageReturn
bl affichageMess
mov x20,0
b 1b // and loop for other entry
3:
bl restauTerm // terminal restaur
ldr x0,qAdrszMessPgmOk // display end message
bl affichageMess
100: // standard end of the program
mov x0,0 // return code
mov x8,EXIT // request to exit program
svc 0 // perform the system call
qAdrszBuffer: .quad szBuffer
qAdrstevents: .quad stevents
qAdrszMessErreur: .quad szMessErreur
qAdrszCarriageReturn: .quad szCarriageReturn
qAdrstOldtio: .quad stOldtio
qAdrstCurtio: .quad stCurtio
qAdrstSigAction: .quad stSigAction
qAdrstSigAction1: .quad stSigAction1
qAdrszMessPgmOk: .quad szMessPgmOk
qAdrSIG_IGN: .quad 1
qAdriEnd: .quad iEnd
qAdriTouche: .quad iTouche
/*********************************/
/* init terminal state */
/*********************************/
initTerm:
stp x1,lr,[sp,-16]! // save registers
/* read terminal state */
mov x0,STDIN // input console
mov x1,TCGETS
ldr x2,qAdrstOldtio
mov x8,IOCTL // call system Linux
svc 0
cbnz x0,98f // error ?
adr x0,sighandler // adresse routine traitement signal
ldr x1,qAdrstSigAction // adresse structure sigaction
str x0,[x1,sa_handler] // maj handler
mov x0,SIGINT // signal type
ldr x1,qAdrstSigAction
mov x2,0
mov x3,8
mov x8,SIGACTION // call system
svc 0
cmp x0,0 // error ?
bne 98f
mov x0,SIGQUIT
ldr x1,qAdrstSigAction
mov x2,0 // NULL
mov x8,SIGACTION // call system
svc 0
cmp x0,0 // error ?
bne 98f
mov x0,SIGTERM
ldr x1,qAdrstSigAction
mov x2,0 // NULL
mov x8,SIGACTION // appel systeme
svc 0
cmp x0,0
bne 98f
//
adr x0,qAdrSIG_IGN // address signal igonre function
ldr x1,qAdrstSigAction1
str x0,[x1,sa_handler]
mov x0,SIGTTOU //invalidate other process signal
ldr x1,qAdrstSigAction1
mov x2,0 // NULL
mov x8,SIGACTION // call system
svc 0
cmp x0,0
bne 98f
//
/* read terminal current state */
mov x0,STDIN
mov x1,TCGETS
ldr x2,qAdrstCurtio // address current termio
mov x8,IOCTL // call systeme
svc 0
cmp x0,0 // error ?
bne 98f
mov x2,ICANON | ECHO // no key pressed echo on display
mvn x2,x2 // and one key
ldr x1,qAdrstCurtio
ldr x3,[x1,#term_c_lflag]
and x3,x2,x2 // add flags
str x3,[x1,#term_c_lflag] // and store
mov x0,STDIN // maj terminal current state
mov x1,TCSETS
ldr x2,qAdrstCurtio
mov x8,IOCTL // call system
svc 0
cbz x0,100f
98: // error display
ldr x1,qAdrszMessErreur // error message
bl displayError
mov x0,-1
100:
ldp x1,lr,[sp],16 // restaur 2 registers
ret // return to address lr x30
qAdrstSigAction2: .quad stSigAction2
qAdrstSigAction3: .quad stSigAction3
/*********************************/
/* init instance epool */
/*********************************/
initPoll:
stp x1,lr,[sp,-16]! // save registers
ldr x0,qAdrstevents
mov x1,STDIN // maj structure events
str x1,[x0,#poll_fd] // maj FD
mov x1,POLLIN // action code
str x1,[x0,#poll_event]
mov x0,0
mov x8,CREATPOLL // create epoll instance
svc 0
cmp x0,0 // error ?
ble 98f
mov x10,x0 // return FD epoll instance
mov x1,EPOLL_CTL_ADD
mov x2,STDIN // Fd to we want add
ldr x3,qAdrstevents // structure events address
mov x8,CTLPOLL // call system control epoll
svc 0
cmp x0,0 // error ?
blt 98f // no
mov x0,x10 // return FD epoll instance
b 100f
98: // error display
ldr x1,qAdrszMessErreur // error message
bl displayError
mov x0,-1
100:
ldp x1,lr,[sp],16 // restaur 2 registers
ret // return to address lr x30
/*********************************/
/* wait key */
/*********************************/
/* x0 contains FD poll */
waitKey:
stp x1,lr,[sp,-16]! // save registers
ldr x11,qAdriTouche // key address
str xzr,[x11] // raz key
1:
ldr x10,qAdriEnd // if signal ctrl-c -> end
ldr x10,[x10]
cbnz x10,100f
ldr x1,qAdrstevents
mov x2,12 // size events
mov x3,1 // timeout = 1 TODO: ??
mov x4,0
mov x8,SYSPOLL // call system wait POLL
svc 0
cmp x0,0 // key pressed ?
bge 100f
98: // error display
ldr x1,qAdrszMessErreur // error message
bl displayError
mov x0,-1
100:
ldp x1,lr,[sp],16 // restaur 2 registers
ret // return to address lr x30
/*********************************/
/* read key */
/*********************************/
/* x0 returns key value */
readKey:
stp x1,lr,[sp,-16]! // save registers
mov x0,STDIN // File Descriptor
ldr x1,qAdriTouche // buffer address
mov x2,BUFSIZE // buffer size
mov x8,READ // read key
svc #0
cmp x0,0 // error ?
ble 98f
ldr x2,qAdriTouche // key address
ldr x0,[x2]
b 100f
98: // error display
ldr x1,qAdrszMessErreur // error message
bl displayError
100:
ldp x1,lr,[sp],16 // restaur 2 registers
ret // return to address lr x30
/*********************************/
/* restaur terminal state */
/*********************************/
restauTerm:
stp x1,lr,[sp,-16]! // save registers
mov x0,STDIN // end then restaur begin state terminal
mov x1,TCSETS
ldr x2,qAdrstOldtio
mov x8,IOCTL // call system
svc 0
cbz x0,100f
ldr x1,qAdrszMessErreur // error message
bl displayError
100:
ldp x1,lr,[sp],16 // restaur 2 registers
ret // return to address lr x30
/******************************************************************/
/* signal processing */
/******************************************************************/
sighandler:
stp x1,lr,[sp,-16]! // save registers
ldr x0,qAdriEnd
mov x1,#1 // maj zone end
str x1,[x0]
ldp x1,lr,[sp],16 // restaur 2 registers
ret // return to address lr x30
/******************************************************************/
/* display error message */
/******************************************************************/
/* x0 contains error code */
/* x1 contains address error message */
displayError:
stp x2,lr,[sp,-16]! // save registers
mov x2,x0 // save error code
mov x0,x1 // display message error
bl affichageMess
mov x0,x2
ldr x1,qAdrsZoneConv // conversion error code
bl conversion10S // decimal conversion
ldr x0,qAdrszMessCodeErr
ldr x1,qAdrsZoneConv
bl strInsertAtCharInc // insert result at @ character
bl affichageMess // display message final
ldp x2,lr,[sp],16 // restaur 2 registers
ret // return to address lr x30
qAdrsZoneConv: .quad sZoneConv
qAdrszMessCodeErr: .quad szMessCodeErr
/********************************************************/
/* File Include fonctions */
/********************************************************/
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"

View file

@ -0,0 +1,308 @@
/* Programme assembleur ARM Raspberry */
/* modèle B 512MO */
/* REMARK 1 : this program use routines in a include file
see task Include a file language arm assembly
for the routine affichageMess conversion10
see at end of this program the instruction include */
/************************************/
/* Constantes */
/************************************/
.equ STDIN, 0 @ Linux input console
.equ STDOUT, 1 @ Linux output console
.equ EXIT, 1 @ Linux syscall
.equ READ, 3 @ Linux syscall
.equ WRITE, 4 @ Linux syscall
.equ IOCTL, 0x36 @ Linux syscall
.equ SIGACTION, 0x43 @ Linux syscall
.equ SYSPOLL, 0xA8 @ Linux syscall
.equ TCGETS, 0x5401
.equ TCSETS, 0x5402
.equ ICANON, 2
.equ ECHO, 10
.equ POLLIN, 1
.equ SIGINT, 2 @ Issued if the user sends an interrupt signal (Ctrl + C)
.equ SIGQUIT, 3 @ Issued if the user sends a quit signal (Ctrl + D)
.equ SIGTERM, 15 @ Software termination signal (sent by kill by default)
.equ SIGTTOU, 22 @
.equ BUFSIZE, 80
/*******************************************/
/* Structures */
/********************************************/
/* structure termios see doc linux*/
.struct 0
term_c_iflag: @ input modes
.struct term_c_iflag + 4
term_c_oflag: @ output modes
.struct term_c_oflag + 4
term_c_cflag: @ control modes
.struct term_c_cflag + 4
term_c_lflag: @ local modes
.struct term_c_lflag + 4
term_c_cc: @ special characters
.struct term_c_cc + 20 @ see length if necessary
term_fin:
/* structure sigaction see doc linux */
.struct 0
sa_handler:
.struct sa_handler + 4
sa_mask:
.struct sa_mask + 4
sa_flags:
.struct sa_flags + 4
sa_sigaction:
.struct sa_sigaction + 4
sa_fin:
/* structure poll see doc linux */
.struct 0
poll_fd: @ File Descriptor
.struct poll_fd + 4
poll_events: @ events mask
.struct poll_events + 4
poll_revents: @ events returned
.struct poll_revents + 4
poll_fin:
/*********************************/
/* Initialized data */
/*********************************/
.data
szMessPgmOk: .asciz "End program OK.\n"
szMessErreur: .asciz "Error detected.\n"
sMessResult: .ascii "Value : "
sMessValeur: .fill 11, 1, ' ' @ size => 11
szCarriageReturn: .asciz "\n"
.align 4
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
.align 4
iEnd: .skip 4 @ 0 loop 1 = end loop
iTouche: .skip BUFSIZE @ value key pressed
stOldtio: .skip term_fin @ old terminal state
stCurtio: .skip term_fin @ current terminal state
stSigAction: .skip sa_fin @ area signal structure
stSigAction1: .skip sa_fin
stPoll1: .skip poll_fin @ area poll structure
stPoll2: .skip poll_fin
szBuffer: .skip BUFSIZE
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: @ entry of program
bl initTerm @ terminal init
mov r2,#0
ldr r3,iAdrszBuffer
1:
bl getKey @ read one key
cmp r0,#113 @ saisie q ?
beq 3f
cmp r0,#81 @ saisie Q ?
beq 3f
cmp r0,#0xA @ <enter> ?
beq 2f
strb r0,[r3,r2] @ store byte in buffer
add r2,#1
b 1b
2: @ display buffer
mov r0,#0 @ store 0 final
strb r0,[r3,r2]
mov r0,r3
bl affichageMess
ldr r0,iAdrszCarriageReturn
bl affichageMess
mov r2,#0
b 1b
3:
bl restauTerm @ terminal restaur
ldr r0,iAdrszMessPgmOk @ display end message
bl affichageMess
100: @ standard end of the program
mov r0, #0 @ return code
mov r7, #EXIT @ request to exit program
svc #0 @ perform the system call
iAdrszBuffer: .int szBuffer
/*********************************/
/* init terminal state */
/*********************************/
initTerm:
push {r0,r1,r2,r7,lr}
/* read terminal state */
mov r0,#STDIN @ input console
mov r1,#TCGETS
ldr r2,iAdrstOldtio
mov r7, #IOCTL @ call system Linux
svc #0
cmp r0,#0 @ error ?
beq 1f
ldr r1,iAdrszMessErreur @ error message
bl displayError
b 100f
1:
adr r0,sighandler @ adresse routine traitement signal
ldr r1,iAdrstSigAction @ adresse structure sigaction
str r0,[r1,#sa_handler] @ maj handler
mov r0,#SIGINT @ signal type
ldr r1,iAdrstSigAction
mov r2,#0 @ NULL
mov r7, #SIGACTION @ call system
svc #0
cmp r0,#0 @ error ?
bne 98f
mov r0,#SIGQUIT
ldr r1,iAdrstSigAction
mov r2,#0 @ NULL
mov r7, #SIGACTION @ call system
svc #0
cmp r0,#0 @ error ?
bne 98f
mov r0,#SIGTERM
ldr r1,iAdrstSigAction
mov r2,#0 @ NULL
mov r7, #SIGACTION @ appel systeme
svc #0
cmp r0,#0
bne 98f
@
adr r0,iAdrSIG_IGN @ address signal igonre function
ldr r1,iAdrstSigAction1
str r0,[r1,#sa_handler]
mov r0,#SIGTTOU @invalidate other process signal
ldr r1,iAdrstSigAction1
mov r2,#0 @ NULL
mov r7,#SIGACTION @ call system
svc #0
cmp r0,#0
bne 98f
@
/* read terminal current state */
mov r0,#STDIN
mov r1,#TCGETS
ldr r2,iAdrstCurtio @ address current termio
mov r7,#IOCTL @ call systeme
svc #0
cmp r0,#0 @ error ?
bne 98f
mov r2,#ICANON | ECHO @ no key pressed echo on display
mvn r2,r2 @ and one key
ldr r1,iAdrstCurtio
ldr r3,[r1,#term_c_lflag]
and r3,r2 @ add flags
str r3,[r1,#term_c_lflag] @ and store
mov r0,#STDIN @ maj terminal current state
mov r1,#TCSETS
ldr r2,iAdrstCurtio
mov r7, #IOCTL @ call system
svc #0
cmp r0,#0
beq 100f
98: @ error display
ldr r1,iAdrszMessErreur @ error message
bl displayError
100:
pop {r0,r1,r2,r7,lr}
bx lr
/*********************************/
/* read key */
/*********************************/
getKey:
push {r1,r2,r7,lr}
ldr r2,iAdriTouche @ key address
mov r0,#0 @ raz key
str r0,[r2]
1:
ldr r0,iAdriEnd @ if signal ctrl-c -> end
ldr r0,[r0]
cmp r0,#0
bne 100f
ldr r0,iAdrstPoll1 @ address structure poll
mov r1,#STDIN
str r1,[r0,#poll_fd] @ maj FD
mov r1,#POLLIN @ action code
str r1,[r0,#poll_events]
mov r1,#1 @ items number structure poll
mov r2,#0 @ timeout = 0
mov r7,#SYSPOLL @ call system POLL
svc #0
cmp r0,#0 @ key pressed ?
ble 1b @ no key pressed -> loop
@ read key
mov r0,#STDIN @ File Descriptor
ldr r1,iAdriTouche @ buffer address
mov r2,#BUFSIZE @ buffer size
mov r7,#READ @ read key
svc #0
cmp r0,#0 @ error ?
ble 98f
ldr r2,iAdriTouche @ key address
ldr r0,[r2]
b 100f
98: @ error display
ldr r1,iAdrszMessErreur @ error message
bl displayError
100:
pop {r1,r2,r7,lr}
bx lr
/*********************************/
/* restaur terminal state */
/*********************************/
restauTerm:
push {r0,r1,r7,lr}
mov r0,#STDIN @ end then restaur begin state terminal
mov r1,#TCSETS
ldr r2,iAdrstOldtio
mov r7,#IOCTL @ call system
svc #0
cmp r0,#0
beq 100f
ldr r1,iAdrszMessErreur @ error message
bl displayError
100:
pop {r1,r2,r7,lr}
bx lr
iAdrsMessValeur: .int sMessValeur
iAdrszMessErreur: .int szMessErreur
iAdrszCarriageReturn: .int szCarriageReturn
iAdrstOldtio: .int stOldtio
iAdrstCurtio: .int stCurtio
iAdrstSigAction: .int stSigAction
iAdrstSigAction1: .int stSigAction1
iAdrszMessPgmOk: .int szMessPgmOk
iAdrsMessResult: .int sMessResult
iAdrSIG_IGN: .int 1
iAdriEnd: .int iEnd
iAdrstPoll1: .int stPoll1
iAdriTouche: .int iTouche
/******************************************************************/
/* signal processing */
/******************************************************************/
sighandler:
push {r0,r1}
ldr r0,iAdriEnd
mov r1,#1 @ maj zone end
str r1,[r0]
pop {r0,r1}
bx lr
/***************************************************/
/* ROUTINES INCLUDE */
/***************************************************/
.include "../affichage.inc"

View file

@ -0,0 +1,7 @@
# syntax: TAWK -f KEYBOARD_INPUT_FLUSH_THE_KEYBOARD_BUFFER.AWK
BEGIN {
while (kbhit()) {
getkey()
}
exit(0)
}

View file

@ -0,0 +1,25 @@
PROC Wait(BYTE frames)
BYTE RTCLOK=$14
frames==+RTCLOK
WHILE frames#RTCLOK DO OD
RETURN
PROC Main()
BYTE CH=$02FC ;Internal hardware value for last key pressed
PrintE("Program is halted for 200 frames.")
PrintE("Type character to fill the buffer.")
Wait(200)
PutE()
DO
IF CH=$FF THEN
PrintE("The buffer is empty.")
EXIT
ELSE
PrintF("The buffer stores internal key: %B.%E",CH)
PrintE("Flush the buffer.")
CH=$FF
FI
OD
RETURN

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 PEEK (49152) > 127 THEN C = PEEK (49168): GOTO 10

View file

@ -0,0 +1,2 @@
while key <> "" : end while
print "Keyboard buffer flushed"

View file

@ -0,0 +1,30 @@
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
// Get a chance to make stdin input buffer dirty.
//
char text[256];
getchar();
// This DOES NOT WORK properly on all modern systems including Linux & W10.
// Obsolete, don't use this. BTW, there is no fpurge in MSVC libs in 2020.
//
// fflush(stdin);
// Always works. Readed characters may remain somethere in RAM.
//
fseek(stdin, 0, SEEK_END);
// A very dirty solution - an unbuffered stream does not need any flushing.
//
// setvbuf(stdin, NULL, _IONBF, 0);
// Now we are able to check if the buffer is really empty.
//
fgets(text, sizeof(text), stdin);
puts(text);
return EXIT_SUCCESS;
}

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,23 @@
#include <conio.h>
#include <tchar.h>
// Uses only CRT functions. No need to include 'Windows.h'.
void Kbflush(void)
{
while (_kbhit())
{
// The _gettch function reads a single character without echoing it. When reading
// a function key or an arrow key, it must be called twice; the first call returns
// 0x00 or 0xE0 and the second call returns the actual key code. Source: MSDN.
int ch = _gettch();
if (ch == 0x00 || ch == 0xE0)
(void)_gettch();
}
}
int _tmain(void)
{
Kbflush();
return 0;
}

View file

@ -0,0 +1,11 @@
10 print chr$(147);chr$(14)
25 get k$:if k$<>"" then 25:rem empty buffer
40 print chr$(19):print " Program halted for 10000 counts. "
45 print:print " Type characters to fill the buffer."
50 for i=1 to 10000:next
60 print:print " Characters in the buffer are:":print:print " ";
65 get k$:print k$;
70 for t=1 to 100:next
75 if k$<>"" then 65
80 print
85 end

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,7 @@
$ wait 0::10 ! gives us 10 seconds to get keystrokes into the type-ahead buffer
$ on control_y then $ goto clean
$ set terminal /noecho
$ loop: read /prompt="" /time=0 sys$command /error=clean buffer
$ goto loop
$ clean:
$ set terminal /echo

View file

@ -0,0 +1,18 @@
program Flush_the_keyboard_buffer;
{$APPTYPE CONSOLE}
uses
Winapi.Windows;
var
StdIn: THandle;
begin
StdIn := GetStdHandle(STD_INPUT_HANDLE);
Writeln('Press any key you want, they will be erased:');
Sleep(5000);
FlushConsoleInputBuffer(StdIn);
Writeln('Now press any key you want, they will NOT be erased:');
readln;
end.

View file

@ -0,0 +1,6 @@
!$KEY
..........
REPEAT
GET(K$)
UNTIL K$=""
..........

View file

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

View file

@ -0,0 +1,6 @@
' FB 1.05.0 Win64
' Get characters from the keyboard buffer until there are none left
While Inkey <> "" : Wend
Print "Keyboard buffer flushed"
Sleep

View file

@ -0,0 +1,16 @@
package main
import (
"log"
gc "code.google.com/p/goncurses"
)
func main() {
_, err := gc.Init()
if err != nil {
log.Fatal("init:", err)
}
defer gc.End()
gc.FlushInput()
}

View file

@ -0,0 +1,34 @@
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
stdin := bufio.NewReader(os.Stdin)
fmt.Println("Please enter an integer: ")
var i1 int
for {
_, err := fmt.Fscan(stdin, &i1)
if err == nil {
break
}
stdin.ReadString('\n')
fmt.Println("Sorry, invalid input. Please enter an integer: ")
flush(stdin)
}
fmt.Println(i1)
}
func flush(reader *bufio.Reader) {
var i2 int
for i2 = 0; i2 < reader.Buffered(); i2++ {
reader.ReadByte()
}
}

View file

@ -0,0 +1,26 @@
import Control.Concurrent (threadDelay)
import Control.Monad (when)
import System.IO (hFlush, stdout)
import System.Posix
-- If the file descriptor is associated with a terminal, then flush its input,
-- otherwise do nothing.
termFlush :: Fd -> IO ()
termFlush fd = do
isTerm <- queryTerminal fd
when isTerm $ discardData fd InputQueue
main :: IO ()
main = do
putStrLn "Type some stuff...\n"
threadDelay $ 3 * 1000000
putStrLn "\n\nOk, stop typing!\n"
threadDelay $ 2 * 1000000
termFlush stdInput
putStr "\n\nType a line of text, ending with a newline: "
hFlush stdout
line <- getLine
putStrLn $ "You typed: " ++ line
termFlush stdInput

View file

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

View file

@ -0,0 +1,9 @@
using Gtk
function flush_keyboard()
win = GtkWindow("", 1, 1)
keyget(w, event) = Int32(0)
signal_connect(keyget, win, "key-press-event")
visible(win, false)
sleep(0.25)
end

View file

@ -0,0 +1,6 @@
// version 1.0.6
fun main(args: Array<String>) {
while (System.`in`.available() > 0) System.`in`.read()
println("Goodbye!")
}

View file

@ -0,0 +1,16 @@
Module Checkit {
\\ feed keyboard
Keyboard "abcd"
\\ Work in Windows not in Linux (no Osk.exe exist)
\\ no error return in linux
Keyboard ! 'open virtual keyboard
Wait 3000
\\ flush keyboard
\\ we can use Do or Repeat (is the same)
Repeat {
a$=inkey$
if a$="" then Print :exit
Print a$;
} Always
}
Checkit

View file

@ -0,0 +1,10 @@
Module checkit {
Print "You have 3 seconds to write your name (press enter)"
After 3000 {
Input End
}
Input "Your name:", A$
If A$="" Then Print "Not Ready" : Exit
Print "Ok:";A$
}
Checkit

View file

@ -0,0 +1,4 @@
const TCIFLUSH: cint = 0
proc tcflush(fd, queue_selector: cint): cint {.header: "termios.h".}
discard tcflush(cint(getFileHandle(stdin)), TCIFLUSH)

View file

@ -0,0 +1,3 @@
import: console
System.Console flush

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,3 @@
-->
<span style="color: #008080;">while</span> <span style="color: #7060A8;">get_key</span><span style="color: #0000FF;">()!=-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span> <span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<!--

View file

@ -0,0 +1,3 @@
while ($Host.UI.RawUI.KeyAvailable) {
$Host.UI.RawUI.ReadKey() | Out-Null
}

View file

@ -0,0 +1 @@
$Host.UI.RawUI.FlushInputBuffer()

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,4 @@
DO WHILE INKEY$ <> ""
LOOP
PRINT "Keyboard buffer flushed"
END

View file

@ -0,0 +1,15 @@
#lang racket
(define-syntax-rule (with-raw body ...)
(let ([saved #f])
(define (stty x) (system (~a "stty " x)) (void))
(dynamic-wind (λ() (set! saved (with-output-to-string (λ() (stty "-g"))))
(stty "raw -echo opost"))
(λ() body ...)
(λ() (stty saved)))))
(with-raw
(printf "Keys pressed from now will be ignored\n")
(sleep 2)
(let loop () (when (char-ready?) (read-char) (loop))) ; flush input
(printf "Now press a key which will not be ignored\n")
(printf "You pressed ~a\n" (read-char)))

View file

@ -0,0 +1,34 @@
use Term::termios;
constant $saved = Term::termios.new( :fd($*IN.native-descriptor) ).getattr;
constant $termios = Term::termios.new( :fd($*IN.native-descriptor) ).getattr;
# set some modified input flags
$termios.unset_iflags(<BRKINT ICRNL ISTRIP IXON>);
$termios.unset_lflags(< ECHO ICANON IEXTEN>);
$termios.setattr(:DRAIN);
# reset terminal to original settings on exit
END { $saved.setattr(:NOW) }
# Sleep for a few seconds to give you time to fill the input buffer,
# type a bunch of random characters.
sleep 2;
# ------- The actual task --------
# Flush the input buffer
$termios.setattr(:FLUSH);
# --------------------------------
# Ctrl-C to exit
loop {
# Read up to 5 bytes from STDIN
# F5 through F12 are 5 bytes each
my $keypress = $*IN.read: 5;
# print the ordinals of the keypress character
print $keypress.decode.ords;
print "|";
}

View file

@ -0,0 +1,3 @@
# Project: Keyboard input/Flush the keyboard buffer
Fflush(stdin)

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,2 @@
while inkey$ <> "" : wend
print "Keyboard buffer flushed"

View file

@ -0,0 +1 @@
def flush() { out.flush() }

View file

@ -0,0 +1,3 @@
while keypressed(KEYBOARD) do
ignore(getc(KEYBOARD));
end while;

View file

@ -0,0 +1,17 @@
var k = frequire('Term::ReadKey');
k.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).
k.ReadMode('cbreak');
# Flush the keyboard in terminal character break mode
while (k.ReadKey(-1) != nil) {
# Do nothing
}
# Don't forget to restore the readmode, when we are finished using the keyboard
k.ReadMode('restore');

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

View file

@ -0,0 +1,4 @@
DO WHILE INKEY$ <> ""
LOOP
PRINT "Keyboard buffer flushed"
END

View file

@ -0,0 +1,3 @@
while read -t 0.01; do
true
done

View file

@ -0,0 +1,9 @@
import "io" for Stdin
System.print("Press some keys followed by enter.")
while (true) {
var b = Stdin.readByte() // reads and removes a key from the buffer
System.print("Removed key with code %(b).")
if (b == 10) break // buffer will be empty when enter key pressed
}
System.print("Keyboard buffer is now empty.")

View file

@ -0,0 +1,2 @@
code OpenI=13;
OpenI(0)