This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,2 @@
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.
[[user input::task| ]]

View file

@ -0,0 +1,2 @@
---
note: Keyboard input

View file

@ -0,0 +1,4 @@
Ch : Character;
Available : Boolean;
Ada.Text_IO.Get_Immediate (Ch, Available);

View file

@ -0,0 +1,2 @@
10 REM k$ will be empty, if no key has been pressed
20 LET k$ = INKEY$

View file

@ -0,0 +1 @@
key$ = INKEY$(0)

View file

@ -0,0 +1 @@
key% = INKEY(0)

View file

@ -0,0 +1,46 @@
#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);
return;
}
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;
FD_ZERO(&fs);
FD_SET(STDIN_FILENO, &fs);
select(STDIN_FILENO + 1, &fs, 0, 0, &tv);
if (FD_ISSET(STDIN_FILENO, &fs)) {
c = getchar();
set_mode(0);
}
return c;
}
int main()
{
int c;
while(1) {
set_mode(1);
while (!(c = get_key())) usleep(10000);
printf("key %d\n", c);
}
}

View file

@ -0,0 +1,6 @@
#include <stdio.h>
void main()
{
char chr = getchar();
}

View file

@ -0,0 +1,16 @@
extern (C) {
void _STI_conio();
void _STD_conio();
int kbhit();
int getch();
}
void main() {
_STI_conio();
char c;
if (kbhit())
c = cast(char)getch();
_STD_conio();
}

View file

@ -0,0 +1,28 @@
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TForm1 = class(TForm)
procedure FormKeyPress(Sender: TObject; var Key: Char);
private
SavedPressedKey: Char;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
begin
SavedPressedKey := Key;
end;
end.

View file

@ -0,0 +1,2 @@
integer key
key = get_key() -- if key was not pressed get_key() returns -1

View file

@ -0,0 +1,2 @@
variable last-key
: check key? if key last-key ! then ;

View file

@ -0,0 +1,7 @@
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")
end

View file

@ -0,0 +1 @@
if key? [make "keyhit readchar]

View file

@ -0,0 +1 @@
(setq *LastKey (key))

View file

@ -0,0 +1,34 @@
#!/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)

View file

@ -0,0 +1,9 @@
/*REXX program demonstrates if any key has been presssed. */
somechar=inkey('nowait')

View file

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

View file

@ -0,0 +1,10 @@
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