Keyboard input/Flush the keyboard buffer: Difference between revisions

From Rosetta Code
Content added Content deleted
m (added whitespace before the TOC (table of contents), added other whitespace to the task's preamble.)
imported>Fth
 
(40 intermediate revisions by 21 users not shown)
Line 16: Line 16:
''The program must not wait for users to type anything.''
''The program must not wait for users to type anything.''
<br><br>
<br><br>
=={{header|6502 Assembly}}==
{{works with|http://skilldrick.github.io/easy6502/ Easy6502}}
On the Easy6502 and 6502asm simulators, the zero page memory address 0xFF is a memory-mapped port that reflects the [[ASCII]] code of last key you pressed on your keyboard.

Writing a value of 0 (or almost any ASCII control code that you can't type on your keyboard) to it effectively flushes the keyboard buffer. This prevents an input loop from thinking your finger is still on that key even after you stopped pressing it.

<syntaxhighlight lang="6502asm">lda #$00
sta $FF</syntaxhighlight>

=={{header|8086 Assembly}}==
{{works with|MS-DOS}}
Depending on the value of <code>AL</code>, you can have this system call "slide" into another related system call immediately after flushing the keyboard buffer. After flushing the keyboard buffer, <code>AL</code> will be copied into <code>AH</code> and then <code>int 21h</code> will be called again. Make sure that any other parameters needed by the interrupt you "slide into" are loaded before flushing, as you won't get a chance to do so between the flush and the second system call!

The valid values of <code>AL</code> for this interrupt are:
* 0x01: Read a character from standard input with echo
* 0x06: Direct console output
* 0x07: Direct character input, no echo
* 0x08: Character input without echo
* 0x0A: Buffered input (for text strings).


If you just want to flush the keyboard buffer without doing anything else, load a zero (or any value that isn't one of the above) into <code>AL</code>.

<syntaxhighlight lang="asm">mov ax,0C00h ;equivalent of "mov ah,0Ch mov al,0"
int 21h</syntaxhighlight>

<syntaxhighlight lang="asm">mov ax,0C0Ah
int 21h ;flush the keyboard buffer then immediately ask the user to type in a sentence and hit Enter when done.</syntaxhighlight>

=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
/* 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"
</syntaxhighlight>
{{output}}
<pre>
abcdefg
help
kk
End program OK.
</pre>

=={{header|Action!}}==
<syntaxhighlight lang="action!">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</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Flush_the_keyboard_buffer.png Screenshot from Atari 8-bit computer]
<pre>
Program is halted for 200 frames.
Type character to fill the buffer.

The buffer stores internal key: 63.
Flush the buffer.
The buffer is empty.
</pre>


=={{header|Ada}}==
=={{header|Ada}}==


<lang Ada>with Ada.Text_IO;
<syntaxhighlight lang="ada">with Ada.Text_IO;
procedure Flushtest is
procedure Flushtest is
use Text_IO;
use Text_IO;
Line 38: Line 482:
Put_Line ("Okay, thanks. Here is some input from you:");
Put_Line ("Okay, thanks. Here is some input from you:");
Put_Line (Get_Line);
Put_Line (Get_Line);
end Flushtest;</lang>
end Flushtest;</syntaxhighlight>

=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
/* 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"
</syntaxhighlight>
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: TAWK -f KEYBOARD_INPUT_FLUSH_THE_KEYBOARD_BUFFER.AWK
BEGIN {
while (kbhit()) {
getkey()
}
exit(0)
}
</syntaxhighlight>


=={{header|Axe}}==
=={{header|Axe}}==
<lang axe>While getKey(0)
<syntaxhighlight lang="axe">While getKey(0)
End</lang>
End</syntaxhighlight>

=={{header|Bash}}==

The <code>-t 0</code> should theoretically work, but it does not seem to for tests that have been tried in cygwin and FreeBSD.

<syntaxhighlight lang="bash">while read -t 0.01; do
true
done</syntaxhighlight>


=={{header|BASIC}}==
=={{header|BASIC}}==


==={{header|Applesoft BASIC}}===
==={{header|Applesoft BASIC}}===
<lang ApplesoftBasic>10 IF PEEK (49152) > 127 THEN C = PEEK (49168): GOTO 10</lang>
<syntaxhighlight lang="applesoftbasic">10 IF PEEK (49152) > 127 THEN C = PEEK (49168): GOTO 10</syntaxhighlight>

==={{header|BASIC256}}===
<syntaxhighlight lang="freebasic">while key <> "" : end while
print "Keyboard buffer flushed"</syntaxhighlight>

==={{header|Commodore BASIC}}===

GET will fetch a single byte from the keyboard buffer, if one is present. The keyboard buffer will hold up to ten bytes before ignoring additional input.

<syntaxhighlight lang="coomodorebasicv2">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</syntaxhighlight>

{{out}}
<pre>
Program halted for 10000 counts.

Type characters to fill the buffer.

Characters in the buffer are:

abCdefGH12

ready.
</pre>


==={{header|Locomotive Basic}}===
==={{header|Locomotive Basic}}===


<lang locobasic>10 CLEAR INPUT</lang>
<syntaxhighlight lang="locobasic">10 CLEAR INPUT</syntaxhighlight>


(Only available in BASIC 1.1 though, i.e. not on the CPC 464.)
(Only available in BASIC 1.1 though, i.e. not on the CPC 464.)

==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
{{works with|True BASIC}}
<syntaxhighlight lang="qbasic">DO WHILE INKEY$ <> ""
LOOP
PRINT "Keyboard buffer flushed"
END</syntaxhighlight>

==={{header|Run BASIC}}===
{{works with|QBasic}}
{{works with|FreeBASIC}}
<syntaxhighlight lang="runbasic">while inkey$ <> "" : wend
print "Keyboard buffer flushed"</syntaxhighlight>

==={{header|True BASIC}}===
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">DO WHILE INKEY$ <> ""
LOOP
PRINT "Keyboard buffer flushed"
END</syntaxhighlight>


==={{header|ZX Spectrum Basic}}===
==={{header|ZX Spectrum Basic}}===
Line 59: Line 889:
There is no need to flush keyboard buffer in Spectrum since key presses are not buffered.
There is no need to flush keyboard buffer in Spectrum since key presses are not buffered.
If a key is currently pressed, the following waits until key is released.
If a key is currently pressed, the following waits until key is released.
<lang basic>10 IF INKEY$ <> "" THEN GO TO 10</lang>
<syntaxhighlight lang="basic">10 IF INKEY$ <> "" THEN GO TO 10</syntaxhighlight>


=={{header|BBC BASIC}}==
=={{header|BBC BASIC}}==
<lang bbcbasic> *FX 15,1</lang>
<syntaxhighlight lang="bbcbasic"> *FX 15,1</syntaxhighlight>
Strictly speaking *FX 15,1 is an Operating System command, but it is emulated in BBC BASIC for Windows. Alternatively the keyboard buffer may be flushed as follows:
Strictly speaking *FX 15,1 is an Operating System command, but it is emulated in BBC BASIC for Windows. Alternatively the keyboard buffer may be flushed as follows:
<lang bbcbasic> REPEAT UNTIL INKEY(0)=-1</lang>
<syntaxhighlight lang="bbcbasic"> REPEAT UNTIL INKEY(0)=-1</syntaxhighlight>
or:
<syntaxhighlight lang="bbcbasic"> REPEAT UNTIL INKEY$(0)=""</syntaxhighlight>


=={{header|C}}==
=={{header|C}}==
===Simple solution for stdin===
<syntaxhighlight lang="c">#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;
}</syntaxhighlight>

===POSIX===
{{libheader|POSIX}}
{{libheader|POSIX}}
Code lifted from [[Keyboard input/Obtain a Y or N response]]:
Code lifted from [[Keyboard input/Obtain a Y or N response]]:
<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdio.h>
#include <stdio.h>
#include <termios.h>
#include <termios.h>
Line 136: Line 1,001:


return 0;
return 0;
}</lang>
}</syntaxhighlight>
===Solution for MSVC conio===
{{libheader|Windows MSVC}}
<syntaxhighlight lang="c">#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;
}</syntaxhighlight>


=={{header|D}}==
=={{header|D}}==
<lang d>extern (C) {
<syntaxhighlight lang="d">extern (C) {
void _STI_conio();
void _STI_conio();
void _STD_conio();
void _STD_conio();
Line 156: Line 1,046:


_STD_conio();
_STD_conio();
}</lang>
}</syntaxhighlight>

=={{header|DCL}}==
=={{header|DCL}}==
<lang>$ wait 0::10 ! gives us 10 seconds to get keystrokes into the type-ahead buffer
<syntaxhighlight lang="text">$ wait 0::10 ! gives us 10 seconds to get keystrokes into the type-ahead buffer
$ on control_y then $ goto clean
$ on control_y then $ goto clean
$ set terminal /noecho
$ set terminal /noecho
Line 164: Line 1,055:
$ goto loop
$ goto loop
$ clean:
$ clean:
$ set terminal /echo</lang>
$ set terminal /echo</syntaxhighlight>
{{out}}
{{out}}
<pre>$ @flush_the_keyboard_buffer ! ignores/discards keystrokes for 10 seconds
<pre>$ @flush_the_keyboard_buffer ! ignores/discards keystrokes for 10 seconds
$</pre>
$</pre>
=={{header|Delphi}}==
{{libheader| Winapi.Windows}}
<syntaxhighlight lang="delphi">
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.</syntaxhighlight>
=={{header|ERRE}}==
=={{header|ERRE}}==
<syntaxhighlight lang="erre">
<lang ERRE>
!$KEY
!$KEY
..........
..........
Line 177: Line 1,088:
UNTIL K$=""
UNTIL K$=""
..........
..........
</syntaxhighlight>
</lang>
Note: Test after K$ can be replaced with <code>LEN(K$)=0</code>.
Note: Test after K$ can be replaced with <code>LEN(K$)=0</code>.


=={{header|Euphoria}}==
=={{header|Euphoria}}==
<lang Euphoria>while get_key()!=-1 do
<syntaxhighlight lang="euphoria">while get_key()!=-1 do
end while</lang>
end while</syntaxhighlight>

=={{header|Forth}}==
<syntaxhighlight lang="forth">
: flush-keys begin key? while key drop repeat ;
</syntaxhighlight>
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64

' Get characters from the keyboard buffer until there are none left
While Inkey <> "" : Wend
Print "Keyboard buffer flushed"
Sleep</syntaxhighlight>


=={{header|Go}}==
=={{header|Go}}==
{{libheader|Curses}}
{{libheader|Curses}}
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 201: Line 1,124:
defer gc.End()
defer gc.End()
gc.FlushInput()
gc.FlushInput()
}</lang>
}</syntaxhighlight>
::<syntaxhighlight lang="go">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()
}
}
</syntaxhighlight>

=={{header|Haskell}}==
This relies upon POSIX terminal support.
<syntaxhighlight lang="haskell">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</syntaxhighlight>

=={{header|i}}==
There is no need to manually flush the keyboard buffer in 'i', the only way to receive keyboard input is in graphics mode and keyboard input is automatically flushed every update.


=={{header|Icon}} and {{header|Unicon}}==
=={{header|Icon}} and {{header|Unicon}}==
The following solution works in both Icon and Unicon.
The following solution works in both Icon and Unicon.
<lang unicon>procedure flushKB()
<syntaxhighlight lang="unicon">procedure flushKB()
while kbhit() do getch() # flush input
while kbhit() do getch() # flush input
end</lang>
end</syntaxhighlight>

=={{header|Julia}}==
{{libheader|Gtk}}
<syntaxhighlight lang="julia">
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
</syntaxhighlight>

=={{header|Kotlin}}==
There appears to be no completely satisfactory, platform independent, way in Java (and hence in the JVM-targetted version of Kotlin) to flush the keyboard buffer. The method presented here may not always work (as the InputStream.available method only gives an estimate of the bytes in the buffer) but is better than nothing and does not block as other approaches to the problem may do.
<syntaxhighlight lang="scala">// version 1.0.6

fun main(args: Array<String>) {
while (System.`in`.available() > 0) System.`in`.read()
println("Goodbye!")
}</syntaxhighlight>

=={{header|M2000 Interpreter}}==
M2000 run in M2000 Environment so has total control, including keyboard. Here we read keyboard from console (we can't read with this way for froms, we have to read from events).

Inkey$ return "" if not key pressed, or the key. Return unicode char

Key$ wait for key, to press, we can use Keyboard to send keys

inkey(1000) wait for a new key to press, we can't use Keyboard to send keys, there is no auto repeat, so if we have a second read inkey(1000) and we keep press the same key, nothing happen (return -1, as no new key)

Keypress(32) we can read if a space key is now pressed. Can't read keyboard if m2000 environment application hasn't focus. It's not a key logger. Keypress(1) read left mouse button, Keypress(2) read right mouse button.


<syntaxhighlight lang="m2000 interpreter">
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
</syntaxhighlight>

This isn't the task. Input ends when statement Input End occur, in a thread.

Statement After make a thread for one time only. When in Input interpreter wait for A$ to input, threads allowed to run. If we write but forget to press enter then input flush. If no input statement run then nothing happen when Input End run.


<syntaxhighlight lang="m2000 interpreter">
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
</syntaxhighlight>

=={{header|MiniScript}}==
{{works with|Mini Micro}}
<syntaxhighlight lang="miniscript">key.clear</syntaxhighlight>


=={{header|Nim}}==
=={{header|Nim}}==
{{libheader|POSIX}}
{{libheader|POSIX}}
<lang nim>const TCIFLUSH: cint = 0
<syntaxhighlight lang="nim">const TCIFLUSH: cint = 0
proc tcflush(fd, queue_selector: cint): cint {.header: "termios.h".}
proc tcflush(fd, queue_selector: cint): cint {.header: "termios.h".}


discard tcflush(cint(getFileHandle(stdin)), TCIFLUSH)</lang>
discard tcflush(cint(getFileHandle(stdin)), TCIFLUSH)</syntaxhighlight>


=={{header|Oforth}}==
=={{header|Oforth}}==


<lang Oforth>import: console
<syntaxhighlight lang="oforth">import: console


System.Console flush</lang>
System.Console flush</syntaxhighlight>


=={{header|Perl}}==
=={{header|Perl}}==


<lang perl>use Term::ReadKey;
<syntaxhighlight lang="perl">use Term::ReadKey;
ReadMode 'restore'; # Flush the keyboard and returns input stream to initial state
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)
# ReadMode 0; # Numerical equivalent of keyboard restore (move comment marker to use instead)
Line 240: Line 1,306:


# Don't forget to restore the readmode, when we are finished using the keyboard
# Don't forget to restore the readmode, when we are finished using the keyboard
ReadMode 'restore';</lang>
ReadMode 'restore';</syntaxhighlight>


=={{header|Phix}}==
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">-->
<lang Phix>while get_key()!=-1 do end while</lang>
<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>
<!--</syntaxhighlight>-->


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
<lang PicoLisp>(while (key 10))</lang>
<syntaxhighlight lang="picolisp">(while (key 10))</syntaxhighlight>


=={{header|PowerShell}}==
=={{header|PowerShell}}==
The following uses the special <code>$Host</code> variable which points to an instance of the PowerShell host application. Since the host's capabilities may vary this may not work in all PowerShell hosts. In particular, this works in the console host, but not in the PowerShell ISE.
The following uses the special <code>$Host</code> variable which points to an instance of the PowerShell host application. Since the host's capabilities may vary this may not work in all PowerShell hosts. In particular, this works in the console host, but not in the PowerShell ISE.
<lang powershell>while ($Host.UI.RawUI.KeyAvailable) {
<syntaxhighlight lang="powershell">while ($Host.UI.RawUI.KeyAvailable) {
$Host.UI.RawUI.ReadKey() | Out-Null
$Host.UI.RawUI.ReadKey() | Out-Null
}</lang>
}</syntaxhighlight>


To flush the keyboard buffer use:
To flush the keyboard buffer use:
<lang powershell>
<syntaxhighlight lang="powershell">
$Host.UI.RawUI.FlushInputBuffer()
$Host.UI.RawUI.FlushInputBuffer()
</syntaxhighlight>
</lang>


=={{header|PureBasic}}==
=={{header|PureBasic}}==
<lang PureBasic>While Inkey(): Wend</lang>
<syntaxhighlight lang="purebasic">While Inkey(): Wend</syntaxhighlight>


=={{header|Python}}==
=={{header|Python}}==
<lang Python>def flush_input():
<syntaxhighlight lang="python">def flush_input():
try:
try:
import msvcrt
import msvcrt
Line 271: Line 1,339:
import sys, termios
import sys, termios
termios.tcflush(sys.stdin, termios.TCIOFLUSH)
termios.tcflush(sys.stdin, termios.TCIOFLUSH)
</syntaxhighlight>
</lang>


=={{header|Racket}}==
=={{header|Racket}}==
Line 277: Line 1,345:
Using <tt>stty</tt> to get the terminal into raw mode.
Using <tt>stty</tt> to get the terminal into raw mode.


<lang racket>
<syntaxhighlight lang="racket">
#lang racket
#lang racket
(define-syntax-rule (with-raw body ...)
(define-syntax-rule (with-raw body ...)
Line 293: Line 1,361:
(printf "Now press a key which will not be ignored\n")
(printf "Now press a key which will not be ignored\n")
(printf "You pressed ~a\n" (read-char)))
(printf "You pressed ~a\n" (read-char)))
</syntaxhighlight>
</lang>

=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2018.12}}
Using termios to set some input attributes, flush the buffer & do unbuffered reads. Longer than strictly necessary to demonstrate concepts and make it easy to verify that it actually works as advertised.
<syntaxhighlight lang="raku" line>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 "|";
}</syntaxhighlight>


=={{header|REXX}}==
=={{header|REXX}}==
This will work for Regina:
This will work for Regina:
<lang rexx>call dropbuf</lang>
<syntaxhighlight lang="rexx">call dropbuf</syntaxhighlight>
This will work for CMS REXX, PC/REXX, Personal REXX, and TSO REXX:
This will work for CMS REXX, PC/REXX, Personal REXX, and TSO REXX:
<lang rexx>'DROPBUF'</lang>
<syntaxhighlight lang="rexx">'DROPBUF'</syntaxhighlight>

=={{header|Ring}}==
<syntaxhighlight lang="ring">
# Project: Keyboard input/Flush the keyboard buffer

Fflush(stdin)
</syntaxhighlight>


=={{header|Ruby}}==
=={{header|Ruby}}==
Line 307: Line 1,421:


{{works with|Ruby|1.9.3}}
{{works with|Ruby|1.9.3}}
<lang ruby>require 'io/console'
<syntaxhighlight lang="ruby">require 'io/console'
$stdin.iflush</lang>
$stdin.iflush</syntaxhighlight>


The other option uses IO#read_nonblock to read the input, without any blocking or waiting. This has a caveat: if the terminal uses the ''canonical input mode'', IO reads only entire lines; and if the input queue contains part of a line, IO#read_nonblock cannot discard this last partial line!
The other option uses IO#read_nonblock to read the input, without any blocking or waiting. This has a caveat: if the terminal uses the ''canonical input mode'', IO reads only entire lines; and if the input queue contains part of a line, IO#read_nonblock cannot discard this last partial line!


<lang ruby>loop { $stdin.read_nonblock(256) } rescue nil</lang>
<syntaxhighlight lang="ruby">loop { $stdin.read_nonblock(256) } rescue nil</syntaxhighlight>


The complete solution calls IO#iflush, or turns off canonical input mode and calls IO#read_nonblock.
The complete solution calls IO#iflush, or turns off canonical input mode and calls IO#read_nonblock.


<lang ruby>class IO
<syntaxhighlight lang="ruby">class IO
def discard_input
def discard_input
icanon = false
icanon = false
Line 379: Line 1,493:
nil
nil
end
end
end</lang>
end</syntaxhighlight>


<lang ruby># Demonstration: discard input, then input a line from user.
<syntaxhighlight lang="ruby"># Demonstration: discard input, then input a line from user.
puts 'Type anything for 2 seconds.'
puts 'Type anything for 2 seconds.'
sleep 2
sleep 2
Line 389: Line 1,503:
then print 'Got line. ', line
then print 'Got line. ', line
else puts 'No line!'
else puts 'No line!'
end</lang>
end</syntaxhighlight>


=={{header|Scala}}==
=={{header|Scala}}==
<lang scala>def flush() { out.flush() }</lang>
<syntaxhighlight lang="scala">def flush() { out.flush() }</syntaxhighlight>

=={{header|Seed7}}==
=={{header|Seed7}}==
The Seed7 library [http://seed7.sourceforge.net/libraries/keybd.htm keybd.s7i]
The Seed7 library [http://seed7.sourceforge.net/libraries/keybd.htm keybd.s7i]
Line 399: Line 1,514:
and [http://seed7.sourceforge.net/libraries/keybd.htm#getc%28in_console_keybd_file%29 getc].
and [http://seed7.sourceforge.net/libraries/keybd.htm#getc%28in_console_keybd_file%29 getc].


<lang seed7>while keypressed(KEYBOARD) do
<syntaxhighlight lang="seed7">while keypressed(KEYBOARD) do
ignore(getc(KEYBOARD));
ignore(getc(KEYBOARD));
end while;</lang>
end while;</syntaxhighlight>


=={{header|Sidef}}==
=={{header|Sidef}}==
{{trans|Perl}}
{{trans|Perl}}
<lang ruby>var k = frequire('Term::ReadKey');
<syntaxhighlight lang="ruby">var k = frequire('Term::ReadKey');


k.ReadMode('restore'); # Flush the keyboard and returns input stream to initial state
k.ReadMode('restore'); # Flush the keyboard and returns input stream to initial state
Line 421: Line 1,536:


# Don't forget to restore the readmode, when we are finished using the keyboard
# Don't forget to restore the readmode, when we are finished using the keyboard
k.ReadMode('restore');</lang>
k.ReadMode('restore');</syntaxhighlight>


=={{header|Tcl}}==
=={{header|Tcl}}==
<lang tcl># No waiting for input
<syntaxhighlight lang="tcl"># No waiting for input
fconfigure stdin -blocking 0
fconfigure stdin -blocking 0
# Drain the data by not saving it anywhere
# Drain the data by not saving it anywhere
Line 430: Line 1,545:


# Flip back into blocking mode (if necessary)
# Flip back into blocking mode (if necessary)
fconfigure stdin -blocking 1</lang>
fconfigure stdin -blocking 1</syntaxhighlight>


=={{header|Vedit macro language}}==
=={{header|Vedit macro language}}==
<lang vedit>Key_Purge()</lang>
<syntaxhighlight lang="vedit">Key_Purge()</syntaxhighlight>

=={{header|Wren}}==
There is currently no direct way to flush the keyboard buffer from Wren-cli.

However, the ''System.readByte()'' method reads and removes the next byte from the buffer, blocking the fiber until ''enter'' is pressed. Consequently, the following code will only flush the buffer if ''enter'' is the last key pressed.

It is necessary to type in some keys first (and have them echoed) to demonstrate that it is in fact working. 'Raw' mode can't be used here as keyboard input is not buffered in that mode.
<syntaxhighlight lang="wren">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.")</syntaxhighlight>

{{out}}
Sample session when the keys a, b, c, d followed by enter are pressed:
<pre>
Press some keys followed by enter.
abcd
Removed key with code 97.
Removed key with code 98.
Removed key with code 99.
Removed key with code 100.
Removed key with code 10.
Keyboard buffer is now empty.
</pre>


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>code OpenI=13;
<syntaxhighlight lang="xpl0">code OpenI=13;
OpenI(0)</lang>
OpenI(0)</syntaxhighlight>

Latest revision as of 14:12, 25 January 2024

Task
Keyboard input/Flush the keyboard buffer
You are encouraged to solve this task according to the task description, using any language you may know.

Flush the   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.

6502 Assembly

Works with: [Easy6502]

On the Easy6502 and 6502asm simulators, the zero page memory address 0xFF is a memory-mapped port that reflects the ASCII code of last key you pressed on your keyboard.

Writing a value of 0 (or almost any ASCII control code that you can't type on your keyboard) to it effectively flushes the keyboard buffer. This prevents an input loop from thinking your finger is still on that key even after you stopped pressing it.

lda #$00
sta $FF

8086 Assembly

Works with: MS-DOS

Depending on the value of AL, you can have this system call "slide" into another related system call immediately after flushing the keyboard buffer. After flushing the keyboard buffer, AL will be copied into AH and then int 21h will be called again. Make sure that any other parameters needed by the interrupt you "slide into" are loaded before flushing, as you won't get a chance to do so between the flush and the second system call!

The valid values of AL for this interrupt are:

  • 0x01: Read a character from standard input with echo
  • 0x06: Direct console output
  • 0x07: Direct character input, no echo
  • 0x08: Character input without echo
  • 0x0A: Buffered input (for text strings).


If you just want to flush the keyboard buffer without doing anything else, load a zero (or any value that isn't one of the above) into AL.

mov ax,0C00h ;equivalent of "mov ah,0Ch mov al,0"
int 21h
mov ax,0C0Ah
int 21h ;flush the keyboard buffer then immediately ask the user to type in a sentence and hit Enter when done.

AArch64 Assembly

Works with: as version Raspberry Pi 3B version Buster 64 bits
/* 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"
Output:
abcdefg
help
kk
End program OK.

Action!

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
Output:

Screenshot from Atari 8-bit computer

Program is halted for 200 frames.
Type character to fill the buffer.

The buffer stores internal key: 63.
Flush the buffer.
The buffer is empty.

Ada

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;

ARM Assembly

Works with: as version Raspberry Pi
/* 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"

AWK

# syntax: TAWK -f KEYBOARD_INPUT_FLUSH_THE_KEYBOARD_BUFFER.AWK
BEGIN {
    while (kbhit()) {
      getkey()
    }
    exit(0)
}

Axe

While getKey(0)
End

Bash

The -t 0 should theoretically work, but it does not seem to for tests that have been tried in cygwin and FreeBSD.

while read -t 0.01; do
    true
done

BASIC

Applesoft BASIC

10  IF  PEEK (49152) > 127 THEN C =  PEEK (49168): GOTO 10

BASIC256

while key <> "" : end while
print "Keyboard buffer flushed"

Commodore BASIC

GET will fetch a single byte from the keyboard buffer, if one is present. The keyboard buffer will hold up to ten bytes before ignoring additional input.

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
Output:
 Program halted for 10000 counts. 

 Type characters to fill the buffer.

 Characters in the buffer are: 

 abCdefGH12

ready.

Locomotive Basic

10 CLEAR INPUT

(Only available in BASIC 1.1 though, i.e. not on the CPC 464.)

QBasic

Works with: QBasic version 1.1
Works with: QuickBasic version 4.5
Works with: True BASIC
DO WHILE INKEY$ <> ""
LOOP
PRINT "Keyboard buffer flushed"
END

Run BASIC

Works with: QBasic
Works with: FreeBASIC
while inkey$ <> "" : wend
print "Keyboard buffer flushed"

True BASIC

Works with: QBasic
DO WHILE INKEY$ <> ""
LOOP
PRINT "Keyboard buffer flushed"
END

ZX Spectrum Basic

There is no need to flush keyboard buffer in Spectrum since key presses are not buffered. If a key is currently pressed, the following waits until key is released.

10 IF INKEY$ <> "" THEN GO TO 10

BBC BASIC

      *FX 15,1

Strictly speaking *FX 15,1 is an Operating System command, but it is emulated in BBC BASIC for Windows. Alternatively the keyboard buffer may be flushed as follows:

      REPEAT UNTIL INKEY(0)=-1

or:

      REPEAT UNTIL INKEY$(0)=""

C

Simple solution for stdin

#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;
}

POSIX

Library: POSIX

Code lifted from Keyboard input/Obtain a Y or N response:

#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;
}

Solution for MSVC conio

Library: Windows MSVC
#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;
}

D

extern (C) {
    void _STI_conio();
    void _STD_conio();
    int kbhit();
    int getch();
}

void main() {
    void flushKB() {
        while (kbhit()) getch();
    }

    _STI_conio();

    flushKB();

    _STD_conio();
}

DCL

$ 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
Output:
$ @flush_the_keyboard_buffer  ! ignores/discards keystrokes for 10 seconds
$

Delphi

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.

ERRE

!$KEY
..........
REPEAT
   GET(K$)
UNTIL K$=""
..........

Note: Test after K$ can be replaced with LEN(K$)=0.

Euphoria

while get_key()!=-1 do
end while

Forth

: flush-keys begin key? while key drop repeat ;

FreeBASIC

' FB 1.05.0 Win64

' Get characters from the keyboard buffer until there are none left
While Inkey <> "" : Wend 
Print "Keyboard buffer flushed"
Sleep

Go

Library: Curses
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()
}
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()
	}
}

Haskell

This relies upon POSIX terminal support.

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

i

There is no need to manually flush the keyboard buffer in 'i', the only way to receive keyboard input is in graphics mode and keyboard input is automatically flushed every update.

Icon and Unicon

The following solution works in both Icon and Unicon.

procedure flushKB()
    while kbhit() do getch()   # flush input
end

Julia

Library: Gtk
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

Kotlin

There appears to be no completely satisfactory, platform independent, way in Java (and hence in the JVM-targetted version of Kotlin) to flush the keyboard buffer. The method presented here may not always work (as the InputStream.available method only gives an estimate of the bytes in the buffer) but is better than nothing and does not block as other approaches to the problem may do.

// version 1.0.6

fun main(args: Array<String>) {
    while (System.`in`.available() > 0) System.`in`.read()
    println("Goodbye!")
}

M2000 Interpreter

M2000 run in M2000 Environment so has total control, including keyboard. Here we read keyboard from console (we can't read with this way for froms, we have to read from events).

Inkey$ return "" if not key pressed, or the key. Return unicode char

Key$ wait for key, to press, we can use Keyboard to send keys

inkey(1000) wait for a new key to press, we can't use Keyboard to send keys, there is no auto repeat, so if we have a second read inkey(1000) and we keep press the same key, nothing happen (return -1, as no new key)

Keypress(32) we can read if a space key is now pressed. Can't read keyboard if m2000 environment application hasn't focus. It's not a key logger. Keypress(1) read left mouse button, Keypress(2) read right mouse button.


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

This isn't the task. Input ends when statement Input End occur, in a thread.

Statement After make a thread for one time only. When in Input interpreter wait for A$ to input, threads allowed to run. If we write but forget to press enter then input flush. If no input statement run then nothing happen when Input End run.


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

MiniScript

Works with: Mini Micro
key.clear

Nim

Library: POSIX
const TCIFLUSH: cint = 0
proc tcflush(fd, queue_selector: cint): cint {.header: "termios.h".}

discard tcflush(cint(getFileHandle(stdin)), TCIFLUSH)

Oforth

import: console

System.Console flush

Perl

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';

Phix

while get_key()!=-1 do end while

PicoLisp

(while (key 10))

PowerShell

The following uses the special $Host variable which points to an instance of the PowerShell host application. Since the host's capabilities may vary this may not work in all PowerShell hosts. In particular, this works in the console host, but not in the PowerShell ISE.

while ($Host.UI.RawUI.KeyAvailable) {
    $Host.UI.RawUI.ReadKey() | Out-Null
}

To flush the keyboard buffer use:

$Host.UI.RawUI.FlushInputBuffer()

PureBasic

While Inkey(): Wend

Python

def flush_input():
    try:
        import msvcrt
        while msvcrt.kbhit():
            msvcrt.getch()
    except ImportError:
        import sys, termios
        termios.tcflush(sys.stdin, termios.TCIOFLUSH)

Racket

Using stty to get the terminal into raw mode.

#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)))

Raku

(formerly Perl 6)

Works with: Rakudo version 2018.12

Using termios to set some input attributes, flush the buffer & do unbuffered reads. Longer than strictly necessary to demonstrate concepts and make it easy to verify that it actually works as advertised.

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 "|";
}

REXX

This will work for Regina:

call dropbuf

This will work for CMS REXX, PC/REXX, Personal REXX, and TSO REXX:

'DROPBUF'

Ring

# Project: Keyboard input/Flush the keyboard buffer

Fflush(stdin)

Ruby

Each terminal device has an input queue for keyboard input. We can either flush this input queue, or read it empty.

Ruby 1.9.3 adds a new library 'io/console', providing IO#iflush to flush and discard the input queue. If its IO object is not a terminal, it raises an error, perhaps Errno::ENODEV.

Works with: Ruby version 1.9.3
require 'io/console'
$stdin.iflush

The other option uses IO#read_nonblock to read the input, without any blocking or waiting. This has a caveat: if the terminal uses the canonical input mode, IO reads only entire lines; and if the input queue contains part of a line, IO#read_nonblock cannot discard this last partial line!

loop { $stdin.read_nonblock(256) } rescue nil

The complete solution calls IO#iflush, or turns off canonical input mode and calls IO#read_nonblock.

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
# 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

Scala

def flush() { out.flush() }

Seed7

The Seed7 library keybd.s7i defines the file KEYBOARD, which provides functions like keypressed and getc.

while keypressed(KEYBOARD) do
  ignore(getc(KEYBOARD));
end while;

Sidef

Translation of: Perl
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');

Tcl

# 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

Vedit macro language

Key_Purge()

Wren

There is currently no direct way to flush the keyboard buffer from Wren-cli.

However, the System.readByte() method reads and removes the next byte from the buffer, blocking the fiber until enter is pressed. Consequently, the following code will only flush the buffer if enter is the last key pressed.

It is necessary to type in some keys first (and have them echoed) to demonstrate that it is in fact working. 'Raw' mode can't be used here as keyboard input is not buffered in that mode.

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.")
Output:

Sample session when the keys a, b, c, d followed by enter are pressed:

Press some keys followed by enter.
abcd
Removed key with code 97.
Removed key with code 98.
Removed key with code 99.
Removed key with code 100.
Removed key with code 10.
Keyboard buffer is now empty.

XPL0

code OpenI=13;
OpenI(0)