Keyboard input/Keypress check: Difference between revisions
m →{{header|PowerShell}}: typo |
|||
Line 36: | Line 36: | ||
=={{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>if ($Host.UI.RawUI.KeyAvailable) { |
<lang powershell>if ($Host.UI.RawUI.KeyAvailable) { |
||
$key = $Host.UI.RawUI.ReadKey() |
$key = $Host.UI.RawUI.ReadKey() |
Revision as of 01:39, 7 March 2011
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
You are encouraged to solve this task according to the task description, using any language you may know.
Determine if a key has been pressed and store this in a variable. If no key has been pressed, the program should continue without waiting.
Ada
<lang Ada>Ch : Character; Available : Boolean;
Ada.Text_IO.Get_Immediate (Ch, Available);</lang>
If key was pressed, Available is set to True and Ch contains the value. If not, Available is set to False.
BASIC
ZX Spectrum Basic
<lang basic>10 REM k$ will be empty, if no key has been pressed 20 LET k$ = INKEY$</lang>
Euphoria
<lang Euphoria>integer key key = get_key() -- if key was not pressed get_key() returns -1</lang>
Forth
<lang forth>variable last-key
- check key? if key last-key ! then ;</lang>
Logo
<lang logo>if key? [make "keyhit readchar]</lang>
PicoLisp
<lang PicoLisp>(setq *LastKey (key))</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>if ($Host.UI.RawUI.KeyAvailable) {
$key = $Host.UI.RawUI.ReadKey()
}</lang>
Python
<lang python>#!/usr/bin/env python
import thread import time
try:
from msvcrt import getch
except ImportError:
def getch(): import sys, tty, termios fd = sys.stdin.fileno() old_settings = termios.tcgetattr(fd) try: tty.setraw(sys.stdin.fileno()) ch = sys.stdin.read(1) finally: termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) return ch
char = None
def keypress():
global char char = getch()
thread.start_new_thread(keypress, ())
while True:
if char is not None: print "Key pressed is " + char break print "Program is running" time.sleep(5)</lang>
PureBasic
Returns a character string if a key is pressed during the call of Inkey(). It doesn't interrupt (halt) the program flow.
If special keys (non-ASCII) have to be handled, RawKey() should be called after Inkey(). <lang PureBasic>k$ = Inkey()</lang>
REXX
The REXX language doesn't have any keyboard tools, but some REXX interpreters have added the functionality via different methods.
<lang rexx>/*REXX program demonstrates if any key has been presssed. */
∙ ∙ ∙
somechar=inkey('nowait')
∙ ∙ ∙</lang>
Tcl
There are two ways to handle listening for a key from the terminal. The first is to put the channel connected to the terminal into non-blocking mode and do a read
on it:
<lang tcl>fconfigure stdin -blocking 0
set ch [read stdin 1]
fconfigure stdin -blocking 1
if {$ch eq ""} {
# Nothing was read
} else {
# Got the character $ch
}</lang> The second method is to set up an event listener to perform callbacks when there is at least one character available: <lang tcl>fileevent stdin readable GetChar proc GetChar {} {
set ch [read stdin 1] if {[eof stdin]} { exit } # Do something with $ch here
}
vwait forever; # run the event loop if necessary</lang> Note that in both cases, if you want to get characters as users actually type them then you have to put the terminal in raw mode. That's formally independent of the actual reading of a character.