[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [CPROG] non blocking tastatur check



On Sun, May 23, 1999 at 04:07:19PM +0200, Thomas M. wrote:
> Hejsa
> 
>  Hvordan pokker læser jeg tastetryk, uden at blokere mit program? Den
> eneste funktion jeg kan finde, er getchar(). Men den blokerer for alt, ind
> til at man har trykket på retur. Jeg har brug for at hente 1 tegn af
> gangen (hvis der er nogle tilgængelige).
> 

Dit problem er at den normale input mode er canonical, i hvilken
terminal I/O foregår på linie basis.  Du skal i starten af dit
program ændre til non-canonical mode, hviket styres af ICANON flaget
i termios.c_lflag. Funktionerne tcgetattr(3) og tcsetattr(3) bruges
til at læse og sætte I/O modes.

Jeg har vedlagt et program, der viser hvordan det kan gøres.

> Thomas Mørch
> licensed ham, with callsign OZ2AFO. License category: CEPT-1
> 
> If God had intended man to smoke He would have set him on fire.

Sune

P.S.  Hvis du skal vide om der er input, kan du bruge select(2).

/*
 * noncanon.c:  Non-canonical I/O mode example.
 */

#include <termios.h>
#include <string.h>
#include <errno.h>

void tc_set_local (int, int, int);

void main(int argc, char *argv[])
{
	int c;

	tc_set_local (2, ICANON, 0); /* clear ICANON */

	printf ("How may I serve you?\n");
	c = getchar (); /* one at a time */
	printf ("\n'%c' it is.\n", c);

	tc_set_local (2, ICANON, 1); /* set ICANON */

	exit (0);
}

void tc_set_local (int fd, int flag, int set)
{
	struct termios tios;
	int res;

	res = tcgetattr (fd, &tios); /* get current settings */
	if (res == -1) {
		fprintf (3, "tcgetattr: %s\n", strerror (errno));
		exit (1);
	}
	if (set) {
		tios.c_lflag |= flag; /* set flag */
	} else {
		tios.c_lflag &= ~(flag); /* clear flag */
	}
	res = tcsetattr (fd, TCSANOW, &tios); /* set new flags */
	if (res == -1) {
		fprintf (3, "tcsetattr: %s\n", strerror (errno));
		exit (1);
	}
}



 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 20:07 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *