Keyboard input/Flush the keyboard buffer: Difference between revisions
m (→{{header|PowerShell}}: typo) |
|||
Line 49: | Line 49: | ||
=={{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 |
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) { |
<lang powershell>while ($Host.UI.RawUI.KeyAvailable) { |
||
$Host.UI.RawUI.ReadKey() | Out-Null |
$Host.UI.RawUI.ReadKey() | Out-Null |
Revision as of 01:38, 7 March 2011
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.
Ada
<lang Ada>with Ada.Text_IO; procedure Flushtest is begin
Ada.Text_IO.Put_Line ("Type anything for 2 s"); Delay 2.0; Flush_Input : declare Ch : Character; More : Boolean; begin loop Ada.Text_IO.Get_Immediate (Ch, More); exit when not More; end loop; end Flush_Input; Ada.Text_IO.Put_Line ("Okay, thanks. Here some input from you:"); Ada.Text_IO.Put_Line (Ada.Text_IO.Get_Line);
end Flushtest;</lang>
BASIC
ZX Spectrum Basic
<lang basic>10 IF INKEY$ <> "" THEN GO TO 10</lang>
C
<lang C>#include <stdlib.h>
- include <stdio.h>
int main(void) {
(void) fflush(stdin); return EXIT_SUCCESS;
}</lang>
Euphoria
<lang Euphoria>while get_key()!=-1 do end while</lang>
PicoLisp
<lang PicoLisp>(while (key 10))</lang>
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.
<lang powershell>while ($Host.UI.RawUI.KeyAvailable) {
$Host.UI.RawUI.ReadKey() | Out-Null
}</lang>
PureBasic
<lang PureBasic>While Inkey(): Wend</lang>
Tcl
<lang 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</lang>