Keyboard input/Flush the keyboard buffer: Difference between revisions

From Rosetta Code
Content added Content deleted
(Add input device as a semantic property. Note that this isn't terminal control, but a form of user input. (And I should pay more attention to my open tabs!))
(added PowerShell)
Line 10: Line 10:


[[Category:Keyboard Input]]
[[Category:Keyboard Input]]

=={{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 wok 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>


=={{header|PureBasic}}==
=={{header|PureBasic}}==

Revision as of 07:39, 19 October 2010

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.

BASIC

ZX Spectrum Basic

<lang basic>10 IF INKEY$ <> "" THEN GOTO 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 wok 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

  1. Drain the data by not saving it anywhere

read stdin

  1. Flip back into blocking mode (if necessary)

fconfigure stdin -blocking 1</lang>