Anonymous user
Keyboard input/Flush the keyboard buffer: Difference between revisions
Keyboard input/Flush the keyboard buffer (view source)
Revision as of 14:12, 25 January 2024
, 4 months ago→{{header|FreeBASIC}}
(Keyboard input/Flush the keyboard buffer in various BASIC dialents (BASIC256, QBasic and True BASIC)) |
imported>Fth |
||
(3 intermediate revisions by 3 users not shown) | |||
Line 22:
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.
<
sta $FF</
=={{header|8086 Assembly}}==
Line 39:
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>.
<
int 21h</
<
int 21h ;flush the keyboard buffer then immediately ask the user to type in a sentence and hit Enter when done.</
=={{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 */
Line 415:
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
{{output}}
<pre>
Line 425:
=={{header|Action!}}==
<
BYTE RTCLOK=$14
frames==+RTCLOK
Line 449:
FI
OD
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Flush_the_keyboard_buffer.png Screenshot from Atari 8-bit computer]
Line 463:
=={{header|Ada}}==
<
procedure Flushtest is
use Text_IO;
Line 482:
Put_Line ("Okay, thanks. Here is some input from you:");
Put_Line (Get_Line);
end Flushtest;</
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
/* Programme assembleur ARM Raspberry */
/* modèle B 512MO */
Line 795:
/***************************************************/
.include "../affichage.inc"
</syntaxhighlight>
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: TAWK -f KEYBOARD_INPUT_FLUSH_THE_KEYBOARD_BUFFER.AWK
BEGIN {
Line 805:
exit(0)
}
</syntaxhighlight>
=={{header|Axe}}==
<
End</
=={{header|Bash}}==
Line 815:
The <code>-t 0</code> should theoretically work, but it does not seem to for tests that have been tried in cygwin and FreeBSD.
<
true
done</
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
<
==={{header|BASIC256}}===
<
print "Keyboard buffer flushed"</
==={{header|Commodore BASIC}}===
Line 832:
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.
<
25 get k$:if k$<>"" then 25:rem empty buffer
40 print chr$(19):print " Program halted for 10000 counts. "
Line 842:
75 if k$<>"" then 65
80 print
85 end</
{{out}}
Line 859:
==={{header|Locomotive Basic}}===
<syntaxhighlight lang
(Only available in BASIC 1.1 though, i.e. not on the CPC 464.)
Line 867:
{{works with|QuickBasic|4.5}}
{{works with|True BASIC}}
<
LOOP
PRINT "Keyboard buffer flushed"
END</
==={{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}}
<
LOOP
PRINT "Keyboard buffer flushed"
END</
==={{header|ZX Spectrum Basic}}===
Line 883 ⟶ 889:
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.
<
=={{header|BBC BASIC}}==
<
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:
<
or:
<
=={{header|C}}==
===Simple solution for stdin===
<
#include <stdlib.h>
Line 923 ⟶ 929:
return EXIT_SUCCESS;
}</
===POSIX===
{{libheader|POSIX}}
Code lifted from [[Keyboard input/Obtain a Y or N response]]:
<
#include <stdio.h>
#include <termios.h>
Line 995 ⟶ 1,001:
return 0;
}</
===Solution for MSVC conio===
{{libheader|Windows MSVC}}
<
#include <tchar.h>
Line 1,020 ⟶ 1,026:
Kbflush();
return 0;
}</
=={{header|D}}==
<
void _STI_conio();
void _STD_conio();
Line 1,040 ⟶ 1,046:
_STD_conio();
}</
=={{header|DCL}}==
<syntaxhighlight lang="text">$ wait 0::10 ! gives us 10 seconds to get keystrokes into the type-ahead buffer
$ on control_y then $ goto clean
$ set terminal /noecho
Line 1,049 ⟶ 1,055:
$ goto loop
$ clean:
$ set terminal /echo</
{{out}}
<pre>$ @flush_the_keyboard_buffer ! ignores/discards keystrokes for 10 seconds
Line 1,055 ⟶ 1,061:
=={{header|Delphi}}==
{{libheader| Winapi.Windows}}
<syntaxhighlight lang="delphi">
program Flush_the_keyboard_buffer;
Line 1,073 ⟶ 1,079:
Writeln('Now press any key you want, they will NOT be erased:');
readln;
end.</
=={{header|ERRE}}==
<syntaxhighlight lang="erre">
!$KEY
..........
Line 1,082 ⟶ 1,088:
UNTIL K$=""
..........
</syntaxhighlight>
Note: Test after K$ can be replaced with <code>LEN(K$)=0</code>.
=={{header|Euphoria}}==
<
end while</
=={{header|Forth}}==
<syntaxhighlight lang="forth">
: flush-keys begin key? while key drop repeat ;
</syntaxhighlight>
=={{header|FreeBASIC}}==
<
' Get characters from the keyboard buffer until there are none left
While Inkey <> "" : Wend
Print "Keyboard buffer flushed"
Sleep</
=={{header|Go}}==
{{libheader|Curses}}
<
import (
Line 1,114 ⟶ 1,124:
defer gc.End()
gc.FlushInput()
}</
::<
import (
Line 1,149 ⟶ 1,159:
}
}
</syntaxhighlight>
=={{header|Haskell}}==
This relies upon POSIX terminal support.
<
import Control.Monad (when)
import System.IO (hFlush, stdout)
Line 1,178 ⟶ 1,188:
line <- getLine
putStrLn $ "You typed: " ++ line
termFlush stdInput</
=={{header|i}}==
Line 1,185 ⟶ 1,195:
=={{header|Icon}} and {{header|Unicon}}==
The following solution works in both Icon and Unicon.
<
while kbhit() do getch() # flush input
end</
=={{header|Julia}}==
{{libheader|Gtk}}
<
using Gtk
Line 1,201 ⟶ 1,211:
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.
<
fun main(args: Array<String>) {
while (System.`in`.available() > 0) System.`in`.read()
println("Goodbye!")
}</
=={{header|M2000 Interpreter}}==
Line 1,224 ⟶ 1,234:
<syntaxhighlight lang="m2000 interpreter">
Module Checkit {
\\ feed keyboard
Line 1,241 ⟶ 1,251:
}
Checkit
</syntaxhighlight>
This isn't the task. Input ends when statement Input End occur, in a thread.
Line 1,248 ⟶ 1,258:
<syntaxhighlight lang="m2000 interpreter">
Module checkit {
Print "You have 3 seconds to write your name (press enter)"
Line 1,259 ⟶ 1,269:
}
Checkit
</syntaxhighlight>
=={{header|MiniScript}}==
{{works with|Mini Micro}}
<syntaxhighlight lang
=={{header|Nim}}==
{{libheader|POSIX}}
<
proc tcflush(fd, queue_selector: cint): cint {.header: "termios.h".}
discard tcflush(cint(getFileHandle(stdin)), TCIFLUSH)</
=={{header|Oforth}}==
<
System.Console flush</
=={{header|Perl}}==
<
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)
Line 1,296 ⟶ 1,306:
# Don't forget to restore the readmode, when we are finished using the keyboard
ReadMode 'restore';</
=={{header|Phix}}==
<!--<
<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>
<!--</
=={{header|PicoLisp}}==
<syntaxhighlight lang
=={{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.
<
$Host.UI.RawUI.ReadKey() | Out-Null
}</
To flush the keyboard buffer use:
<
$Host.UI.RawUI.FlushInputBuffer()
</syntaxhighlight>
=={{header|PureBasic}}==
<syntaxhighlight lang
=={{header|Python}}==
<
try:
import msvcrt
Line 1,329 ⟶ 1,339:
import sys, termios
termios.tcflush(sys.stdin, termios.TCIOFLUSH)
</syntaxhighlight>
=={{header|Racket}}==
Line 1,335 ⟶ 1,345:
Using <tt>stty</tt> to get the terminal into raw mode.
<
#lang racket
(define-syntax-rule (with-raw body ...)
Line 1,351 ⟶ 1,361:
(printf "Now press a key which will not be ignored\n")
(printf "You pressed ~a\n" (read-char)))
</syntaxhighlight>
=={{header|Raku}}==
Line 1,357 ⟶ 1,367:
{{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"
constant $saved = Term::termios.new( :fd($*IN.native-descriptor) ).getattr;
Line 1,390 ⟶ 1,400:
print $keypress.decode.ords;
print "|";
}</
=={{header|REXX}}==
This will work for Regina:
<syntaxhighlight lang
This will work for CMS REXX, PC/REXX, Personal REXX, and TSO REXX:
<syntaxhighlight lang
=={{header|Ring}}==
<
# Project: Keyboard input/Flush the keyboard buffer
Fflush(stdin)
</syntaxhighlight>
=={{header|Ruby}}==
Line 1,411 ⟶ 1,421:
{{works with|Ruby|1.9.3}}
<
$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!
<
The complete solution calls IO#iflush, or turns off canonical input mode and calls IO#read_nonblock.
<
def discard_input
icanon = false
Line 1,483 ⟶ 1,493:
nil
end
end</
<
puts 'Type anything for 2 seconds.'
sleep 2
Line 1,493 ⟶ 1,503:
then print 'Got line. ', line
else puts 'No line!'
end</
=={{header|Scala}}==
<
=={{header|Seed7}}==
Line 1,504 ⟶ 1,514:
and [http://seed7.sourceforge.net/libraries/keybd.htm#getc%28in_console_keybd_file%29 getc].
<
ignore(getc(KEYBOARD));
end while;</
=={{header|Sidef}}==
{{trans|Perl}}
<
k.ReadMode('restore'); # Flush the keyboard and returns input stream to initial state
Line 1,526 ⟶ 1,536:
# Don't forget to restore the readmode, when we are finished using the keyboard
k.ReadMode('restore');</
=={{header|Tcl}}==
<
fconfigure stdin -blocking 0
# Drain the data by not saving it anywhere
Line 1,535 ⟶ 1,545:
# Flip back into blocking mode (if necessary)
fconfigure stdin -blocking 1</
=={{header|Vedit macro language}}==
<syntaxhighlight lang
=={{header|Wren}}==
Line 1,546 ⟶ 1,556:
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.
<
System.print("Press some keys followed by enter.")
Line 1,554 ⟶ 1,564:
if (b == 10) break // buffer will be empty when enter key pressed
}
System.print("Keyboard buffer is now empty.")</
{{out}}
Line 1,570 ⟶ 1,580:
=={{header|XPL0}}==
<
OpenI(0)</
|