Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!think.com!spool.mu.edu!olivea!uunet!seismo!beno!black From: black@beno.CSS.GOV (Mike Black) Newsgroups: comp.sys.amiga.programmer Subject: Re: Checking for a Keystroke (in C) Message-ID: <49724@seismo.CSS.GOV> Date: 22 Jun 91 13:56:18 GMT Article-I.D.: seismo.49724 References: <1991Jun19.131033.14578@schaefer.math.wisc.edu> <2676@winnie.fit.edu> Sender: usenet@seismo.CSS.GOV Distribution: usa Organization: Center for Seismic Studies, Arlington, VA Lines: 94 In article <1991Jun19.131033.14578@schaefer.math.wisc.edu> mueller@math.wisc.edu writes: >This question probably will require a somewhat lengthy answer. I hope that >someone out there will take the time. > >I have the SAS/C compiler for the AMIGA and I want to write a routine that >I can call that will check for a keypress. If a key is being pressed, I want >the routine to report the key being pressed. If not I want it to return -1 >or some such thing. Can anyone tell me how to do this (or direct me to some >source code that does this)? I have a feeling that I'm going to have to use >the keyboard device or the input device or the console device. If so, I'm >really going to need some help getting started. > Here's what I use for Aztec C...I believe it should work on SAS also: P.S. This is a little demo that allows the numeric keypad to act like cursor control until 'q' is pressed at which time you will see a stream of periods run across the screen until you press 'q' again. This demonstrates the use of getchar() under RAW i/o and the small inkey() function that I added. #include #include #include main() { char c; int x,y; raw(1); /* turn on raw mode */ puts("Enter 'q' to quit"); while((c=getchar()) != 'q') { switch (c) { case '8': printf("%c%c%c",27,'[','A'); /* cursor up */ break; case '2': printf("%c%c%c",27,'[','B'); /* cursor down */ break; case '6': printf("%c%c%c",27,'[','C'); /* cursor right */ break; case '4': printf("%c%c%c",27,'[','D'); /* cursor left */ break; case '0': printf("%c%c%c",27,'[','H'); /* cursor home */ break; case '.': printf("Enter x y:"); raw(0); /* turn off so we can see the x y on screen */ scanf("%d%d",&x,&y); raw(1); printf("%c[%d;%dH",27,x,y); break; case '-': printf("%c",12); /* clear screen */ break; } fflush(stdout); } /* test inkey function */ while((c=inkey()) != 'q') {printf("%c.",c);fflush(stdout);} raw(0); putchar(12); /* just to neaten the screen a little */ return 0; } raw(int flag) { static struct sgttyb stty,sttysave; static int saved; if (flag && !saved) { ioctl(fileno(stdin),TIOCGETP,&sttysave); ioctl(fileno(stdin),TIOCGETP,&stty); stty.sg_flags |= RAW; ioctl(fileno(stdin),TIOCSETP,&stty); saved=1; } else { if (saved) { ioctl(fileno(stdin),TIOCSETP,&sttysave); saved = 0; } } } int inkey(void) /* returns 0 if no key available */ { if (WaitForChar(Input(),1)) return getchar(); else return 0; } -- ------------------------------------------------------------------------------- : usenet: black@beno.CSS.GOV : land line: 407-494-5853 : I want a computer: : real home: Melbourne, FL : home line: 407-242-8619 : that does it all!: -------------------------------------------------------------------------------