🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: pl.comp.lang.perl
1 messages
1 total messages Started by Piotr Roszatycki Thu, 09 Jan 1997 19:32
if( kbhit() ) w perlu
#25
Author: Piotr Roszatycki
Date: Thu, 09 Jan 1997 19:32
105 lines
2712 bytes
Udalo mi sie :)

Po nieprzespanej nocy mam obsluge klawiatury bez zadnych funkcji ioctl,
fnctl, laczenia perla z C, XS, itd. Czysty perl!
Okazalo sie to strasznie banalne. Zreszta zapodaje sursa, moze sie komus
przyda?

#!/usr/bin/perl
#
# getkey.pl
# (c) 1997 Dexter!dexter@dedal.man.szczecin.pl
#
# wymaga programu "stty"


# &init_getkey
# Przelacza terminal w odpowiedni tryb
#
sub init_getkey {
    $oldstty = `stty -g` || "sane";   # zapamietuje poprzedni stan
terminala
                                      # lub "sane" - standardowe
ustawienia
    system "stty raw -echo";          # wczytuje od teraz bez echa i
znakow kontrolnych
}


# &done_getkey
# Przywraca poprzedni stan terminala
#
sub done_getkey {
    system "stty $oldstty";
}


# &kbhit( strumien )
# Powrot:
#   0 - brak wcisnietych klawiszy, 1 - wcisnieto klawisz
#
sub kbhit {
    local($fd) = shift || fileno(STDIN);
    local($rin) = '';
    vec( $rin, $fd, 1 ) = 1;
    local($nfound) = select( $rin, undef, undef, 0 );
    $nfound;
}


# &getch( strumien )
# Powrot:
#   kod klawisza
#
sub getch {
    local($fd) = shift || fileno(STDIN);
    local($key);
    sysread(STDIN,$key,1);
    $key;
}

1;

---------koniec---------

I oto jak to mozna wykorzystac:

#!/usr/bin/perl

require 'getkey.pl';


$| = 1;                 # musi byc autoflush, by nie buforowal print'y

&init_getkey;

$i = 0;

do {
    if( ++$i >= 100 ) {
        $i = 0;
        print ".";
    }
    if( &kbhit ) {
        $key = &getch;
        print "\r\n$key\r\n";   # bo teraz nie tlumaczy \n na \r\n !
    }
} until( $key eq 'q' );

&done_getkey;

---------koniec---------


No wlasnie, wykorzystuje "stty", i tylko zastanawiam sie, czy jest to na
tyle standardowe narzedzie, ze odpali sie na kazdym systemie...
Sprawdzilem, chodzi na Linux'ie i IRIX'ie. Jesli ktos ma dostep do
innych systemow, prosze o kontakt przez mail'a, jesli cos jest nie tak.

Teraz mozna pokusic sie o "conio.pl" ;)

-- 
Piotr "Dexter" Roszatycki
http://www.szczecin.pl/~dexter
mailto:dexter@dedal.man.szczecin.pl
irc:Dexter#SzczeciN
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads