Keyboard input/Keypress check

From Rosetta Code
Jump to: navigation, search
Keyboard input/Keypress check
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.


[edit] Ada

Ch : Character;
Available : Boolean;
Ada.Text_IO.Get_Immediate (Ch, Available);

If key was pressed, Available is set to True and Ch contains the value. If not, Available is set to False.

[edit] AutoHotkey

Waits for the user to type a string (not supported on Windows 9x: it does nothing).

; Input [, OutputVar, Options, EndKeys, MatchList]
Input, KeyPressed, L1 T2 ; Length = 1, Timeout = 2 seconds

Checks if a keyboard key or mouse/joystick button is down or up. Also retrieves joystick status.

; GetKeyState, OutputVar, KeyName [, Mode]
GetKeyState, State, RButton ; Right mouse button.

Function version of GetKeyState.

; KeyIsDown := GetKeyState("KeyName" [, "Mode"])
State := GetKeyState("RButton", "P") ; Right mouse button. P = Physical state.

[edit] Axe

Note that while the syntax for getting the most recent keypress is identical to TI-83 BASIC, the keycodes themselves are different.


[edit] BASIC

[edit] Applesoft BASIC

K = PEEK(-16384) : IF K > 127 THEN POKE -16368,0 : K$ = CHR$(K)

[edit] ZX Spectrum Basic

Works with: Locomotive Basic
10 REM k$ will be empty, if no key has been pressed
20 LET k$ = INKEY$

[edit] BBC BASIC

      key$ = INKEY$(0)

If there was no keypress an empty string is returned. Alternatively a numeric key-code may be obtained; if there was no keypress -1 is returned:

      key% = INKEY(0)

[edit] C

For POSIX systems. Ctrl-C to stop:
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
void set_mode(int want_key)
static struct termios old, new;
if (!want_key) {
tcsetattr(STDIN_FILENO, TCSANOW, &old);
tcgetattr(STDIN_FILENO, &old);
new = old;
new.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &new);
int get_key()
int c = 0;
struct timeval tv;
fd_set fs;
tv.tv_usec = tv.tv_sec = 0;
select(STDIN_FILENO + 1, &fs, 0, 0, &tv);
c = getchar();
return c;
int main()
int c;
while(1) {
while (!(c = get_key())) usleep(10000);
printf("key %d\n", c);

you could just use getchar()

#include <stdio.h>
void main()
char chr = getchar();

[edit] C#

string chr = string.Empty;
chr = Console.ReadKey().Key.ToString();

[edit] Clojure

Library: jline

Note: If you run it with Leiningen, use the special trampoline run to prevent issues:

$ lein trampoline run
(ns keypress.core
(:import jline.Terminal)
(def keypress (future (.readCharacter (Terminal/getTerminal) System/in)))
(defn prompt []
(println "Awaiting char...\n")
(Thread/sleep 2000)
(if-not (realized? keypress)
(println "key: " (char @keypress))))
(defn -main [& args]

[edit] D

extern (C) {
void _STI_conio();
void _STD_conio();
int kbhit();
int getch();
void main() {
char c;
if (kbhit())
c = cast(char)getch();

[edit] Delphi

This is valid for a GUI application!

unit Unit1;
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
TForm1 = class(TForm)
procedure FormKeyPress(Sender: TObject; var Key: Char);
SavedPressedKey: Char;
Form1: TForm1;
{$R *.dfm}
procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
SavedPressedKey := Key;

[edit] ERRE


Note: If no key was pressed K$ is empty string "".

[edit] Euphoria

integer key
key = get_key() -- if key was not pressed get_key() returns -1

[edit] F#

Translation of: C#
open System;
let chr = if Console.KeyAvailable then Console.ReadKey().Key.ToString() else String.Empty

[edit] Forth

variable last-key
: check key? if key last-key ! then ;

[edit] Go

Library: Curses
package main
import (
gc ""
func main() {
s, err := gc.Init()
if err != nil {
log.Fatal("init:", err)
defer gc.End()
s.Move(20, 0)
s.Print("Key check in ")
for i := 3; i >= 1; i-- {
s.MovePrint(20, 13, i)
time.Sleep(500 * time.Millisecond)
// task requirement next two lines
k := s.GetChar()
if k == 0 {
s.Println("No key pressed")
} else {
s.Println("You pressed", gc.KeyString(k))

[edit] Haskell

import Control.Concurrent
import Control.Monad
import Data.Maybe
import System.IO
main = do
c <- newEmptyMVar
hSetBuffering stdin NoBuffering
forkIO $ do
a <- getChar
putMVar c a
putStrLn $ "\nChar '" ++ [a] ++
"' read and stored in MVar"
wait c
where wait c = do
a <- tryTakeMVar c
if isJust a then return ()
else putStrLn "Awaiting char.." >>
threadDelay 500000 >> wait c


Awaiting char..
Awaiting char..
Awaiting char..
Char 'd' read and stored in MVar

[edit] Icon and Unicon

procedure main()
delay(1000) # give user a chance to input
if kbhit() then # test for input
write("You entered ",x := getch()) # read w/o echo
else # use getche for echo
write("No input waiting")


if key? [make "keyhit readchar]

[edit] Oforth

func: checkKey 
| key |
System.Console receiveTimeout(2000000) ->key // Wait a key pressed for 2 seconds
key ifNotNull: [ System.Out "Key pressed : " << key << cr ]
"Done" println

Other options :

System.Console receive ->key                 // Wait until a key is pressed ( = receiveTimeout(null) )
System.Console receiveChar ->aChar // Wait until a character is pressed. All other keys are ignored
System.Console receiveTimeout(0) ->key // Check if a key is pressed and return immediatly

[edit] PicoLisp

(setq *LastKey (key))

[edit] 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.

if ($Host.UI.RawUI.KeyAvailable) {
$key = $Host.UI.RawUI.ReadKey()

[edit] Python

#!/usr/bin/env python
# this solution will work only in Windows, as msvcrt is a Windows only package
import thread
import time
from msvcrt import getch # try to import Windows version
except ImportError:
def getch(): # define non-Windows version
import sys, tty, termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
ch =
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
print "Program is running"

[edit] 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().

k$ = Inkey()

[edit] 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)))))
(printf "Press a key, or not\n")
(sleep 2)
(if (char-ready?)
(printf "You pressed ~a\n" (read-char))
(printf "You didn't press a key\n")))

[edit] REXX

The REXX language doesn't have any keyboard tools, but some REXX interpreters have added the functionality via different methods.

This version only works with:

  • Personal REXX
/*REXX program demonstrates if any key has been presssed.               */


[edit] Seed7

The library keybd.s7i defines the file KEYBOARD and the function keypressed, which can be used to determine if a key has been pressed.

if keypressed(KEYBOARD) then
writeln("A key was pressed");
writeln("No key was pressed");
end if;

[edit] 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:

fconfigure stdin -blocking 0
set ch [read stdin 1]
fconfigure stdin -blocking 1
if {$ch eq ""} {
# Nothing was read
} else {
# Got the character $ch

The second method is to set up an event listener to perform callbacks when there is at least one character available:

fileevent stdin readable GetChar
proc GetChar {} {
set ch [read stdin 1]
if {[eof stdin]} {
# Do something with $ch here
vwait forever; # run the event loop if necessary

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.

[edit] TI-83 BASIC

TI-83 BASIC has a built in getKey function.


This returns the key code of the key pressed which is the row number followed by the column number. The left up and down arrow keys are grouped with row 2 as 24, 25, and 26, and the down arrow key is grouped with row 3 as 34.

[edit] XPL0

include c:\cxpl\codes;  \intrinsic 'code' declarations
int K, N;
[N:= 0;
repeat K:= KeyHit; \counts up until a key is pressed
IntOut(0, N); ChOut(0, ^ );
N:= N+1;
until K;
Personal tools