⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 kbhit.txt

📁 this is a mirrored site c-faq. thought might need offline
💻 TXT
📖 第 1 页 / 共 2 页
字号:
    if (kbhit()) {      c = getch();      mvprintw(1, 0, "%d: \'%c\' is pressed (count: %ld)\n", ++i, c, count);      refresh();      }    }  nocrmode();  echo();  endwin();        /* close curses... */}/* ---- Cut Here ---- */--Han Yun-Su, KAIST, Life Science    |   Life is short, DNA is long.Molecular Genetics Laboratory      |   DNAs, recombine in the neuclus!Tel: (042)-4061, 4786              |   Slow and steady evolves the life.Article 47456 of comp.lang.c:Path: mdisea!uw-coco!uw-beaver!nntp.cs.ubc.ca!unixg.ubc.ca!erich.triumf.ca!advaxFrom: advax@erich.triumf.ca (A.Daviel)Newsgroups: comp.lang.cSubject: getch() for VMSDate: 15 Nov 1993 12:43 PSTOrganization: TRIUMF: Tri-University Meson FacilityLines: 34Distribution: worldMessage-ID: <15NOV199312432724@erich.triumf.ca>NNTP-Posting-Host: erich.triumf.caNews-Software: VAX/VMS VNEWS 1.41    I see in the FAQ that this is a frequently asked question (How does one doa DOS-type getch() on machine yy?). I wrote this for VMS; I don't pretend that it is fast, elegant or even correct, but it seems to work./* function getch */#include iodef  /* Input/Output defines */#include descrip /* VMS string descriptors */char getch(){int iosb[2] ; /* I/O status block */static int ichan = 0 ; $DESCRIPTOR (lun,"SYS$INPUT") ;char ikey ;const read_noecho = IO$_READVBLK | IO$M_NOECHO ;int stat ;/* initialize channel if required */if (ichan==0)  {  stat = sys$assign(&lun,&ichan,0,0) ;   if (stat!=1) lib$signal(stat) ;  }/* read one key */stat = sys$qiow(0,ichan,read_noecho,&iosb,0,0,&ikey,1,0,0,0,0) ;if (stat!=1) lib$signal(stat) ;return (ikey) ;}-- Andrew Daviel, Vancouver, Canada <advax@triumf.ca>  TRIUMF - home of the world's largest cyclotronArticle 14000 of comp.unix.programmer:Path: mdisea!mmddvan!vanbc.wimsey.com!cyber2.cyberstore.ca!nntp.cs.ubc.ca!destroyer!caen!math.ohio-state.edu!cs.utexas.edu!uunet!pitt.edu!neurocog.lrdc.pitt.edu!hahnFrom: hahn@neurocog.lrdc.pitt.edu (Mark Hahn)Newsgroups: comp.unix.programmerSubject: Re: getchar() without waiting for '\n'Message-ID: <7332@blue.cis.pitt.edu>Date: 18 Nov 93 17:34:32 GMTReferences: <umshih02.751856583@silver.cs.umanitoba.ca> <1993Nov14.101627.7636@greco-prog.fr> <CGn5BA.FoM@cs.vu.nl>Sender: news+@pitt.eduLines: 30X-Newsreader: TIN [version 1.2 PL2]> : Jun Shih (umshih02@silver.cs.umanitoba.ca) wrote:> : : How can I make getchar() to get a single character with waiting for> : : the newline (CR) char?   So after a user type a char, my progaram will> : : respond right away.here's a better and very portable way to do it:#include <stdio.h>#include <termios.h>int main() {    struct termios t, tOrg;    char ch;    tcgetattr(fileno(stdin), &t);    tOrg = t;    t.c_lflag &= ~(ECHO | ICANON);    t.c_cc[VMIN] = 1;    tcsetattr(fileno(stdin),TCSANOW,&t);    while((ch = getchar()) != 'q') {        fprintf(stderr,"character typed: %c\n", ch);    }    tcsetattr(fileno(stdin),TCSANOW,&tOrg);    return 0;}regards, mark hahn.--this space intentionally left non-blank.        hahn@neurocog.lrdc.pitt.eduArticle 64646 of comp.lang.c:Path: mdisea!mmddvan!vanbc.wimsey.com!cyber2.cyberstore.ca!math.ohio-state.edu!uwm.edu!msuinfo!harbinger.cc.monash.edu.au!giaeb!leeFrom: lee@giaeb.cc.monash.edu.au (Lee Hollingworth)Newsgroups: comp.lang.cSubject: Re: VT100 - Immediate IO (on UNIX)Date: 12 Jun 94 02:43:05 GMTOrganization: Monash UniversityLines: 97Message-ID: <lee.771388985@giaeb>References: <andrewb-120694113321@n160234.otago.ac.nz>NNTP-Posting-Host: giaeb.cc.monash.edu.auX-Newsreader: NN version 6.5.0 #4 (NOV)andrewb@atlas.otago.ac.nz (A Blakie) writes:>Does anybody know how we can read characters from the keyboard without>waiting>for a carriage return? If you have any solutions to my problem, or any>commands>that I may have overlooked to do this, then could you please write and tell>me.>ps : do these solutions wait until somebody presses a key? or just do a>quick>check (cf peek in basic)?Here is my rendition, do with it what you may :-)Lee.#include <unistd.h>     // read()#include <fcntl.h>      // setting keyboard flags#include <sys/ioctl.h>#include <termio.h>     // used to set terminal modes#include <termios.h>//// two global variables for tty and keybrd control//static struct termio term_orig;static int kbdflgs;//// function :   system_mode// purpose  :   reset the system to what it was before input_mode was//              called//void system_mode(void){    if (ioctl(0, TCSETA, &term_orig) == -1) {        return;    }    fcntl(0, F_SETFL, kbdflgs);}//// function :   input_mode// purpose  :   set the system into raw mode for keyboard i/o// returns  :   0 - error//              1 - no error//int input_mode(void){    struct termio term;    // to avoid ^S ^Q processing    //    // get rid of XON/XOFF handling, echo, and other input processing    //    if (ioctl(0, TCGETA, &term) == -1) {        return (0);    }    (void) ioctl(0, TCGETA, &term_orig);    term.c_iflag = 0;    term.c_oflag = 0;    term.c_lflag = 0;    term.c_cc[VMIN] = 1;    term.c_cc[VTIME] = 0;    if (ioctl(0, TCSETA, &term) == -1) {        return (0);    }    kbdflgs = fcntl(0, F_GETFL, 0);    //    // no delay on reading stdin    //    int flags = fcntl(0, F_GETFL);    flags &= ~O_NDELAY;    fcntl(0, F_SETFL, flags);    return (1);}//// function :   getch// purpose  :   read a single character from the keyboard without echo// returns  :   the keybress character//int getch(void){    unsigned char ch;    input_mode();    while (read(0, &ch, 1) != 1) ;    system_mode();    return (ch);}int main(){    return (getch());}Article 65878 of comp.lang.c:Path: mdisea!mmddvan!vanbc.wimsey.com!deep.rsoft.bc.ca!sol.ctr.columbia.edu!howland.reston.ans.net!vixen.cso.uiuc.edu!uwm.edu!msuinfo!harbinger.cc.monash.edu.au!giaeb!leeFrom: lee@giaeb.cc.monash.edu.au (Lee Hollingworth)Newsgroups: comp.lang.cSubject: Re: getchar()Date: 25 Jun 94 10:07:29 GMTOrganization: Monash UniversityLines: 104Message-ID: <lee.772538849@giaeb>References: <2uftsr$c9f@news.u.washington.edu>NNTP-Posting-Host: giaeb.cc.monash.edu.auKeywords: getcharX-Newsreader: NN version 6.5.0 #4 (NOV)jeffse@u.washington.edu (Jeffrey Seifer) writes:>Could someone please help me...>I have been writing c for years on IBM pc's, and now I am writing>under unix.  I have always used the function getch() to get a single>character from stdin (getchar() waits for you to press return).  Is>there a way to do this under unix?The trouble is that reading without a CR is ver O/S specific.Here is some code which does what you want and should compile onmost (?) UNIX boxes...(It was written for HP-UX 8.02)Lee Hollingworthlee@giaeb.cc.monash.edu.au#include <unistd.h>     // read()#include <fcntl.h>      // setting keyboard flags#include <sys/ioctl.h>#include <termio.h>     // used to set terminal modes#include <termios.h>//// two global variables for tty and keybrd control//static struct termio term_orig;static int kbdflgs;//// function :   system_mode// purpose  :   reset the system to what it was before input_mode was//              called//void system_mode(void){    if (ioctl(0, TCSETA, &term_orig) == -1) {        return;    }    fcntl(0, F_SETFL, kbdflgs);}//// function :   input_mode// purpose  :   set the system into raw mode for keyboard i/o// returns  :   0 - error//              1 - no error//int input_mode(void){    struct termio term;    // to avoid ^S ^Q processing    //    // get rid of XON/XOFF handling, echo, and other input processing    //    if (ioctl(0, TCGETA, &term) == -1) {        return (0);    }    (void) ioctl(0, TCGETA, &term_orig);    term.c_iflag = 0;    term.c_oflag = 0;    term.c_lflag = 0;    term.c_cc[VMIN] = 1;    term.c_cc[VTIME] = 0;    if (ioctl(0, TCSETA, &term) == -1) {        return (0);    }    kbdflgs = fcntl(0, F_GETFL, 0);    //    // no delay on reading stdin    //    int flags = fcntl(0, F_GETFL);    flags &= ~O_NDELAY;    fcntl(0, F_SETFL, flags);    return (1);}//// function :   getch// purpose  :   read a single character from the keyboard without echo// returns  :   the keybress character//int getch(void){    //    // no delays on reading stdin    //    input_mode();    //    // do a simple loop and get the response    //    unsigned char ch;    while (read(0, &ch, 1) != 1) ;    system_mode();    return (ch);}int main(){    return (getch());}Article 32001 of comp.unix.programmer:Path: mdisea!mmddvan!vanbc.wimsey.com!io.org!interlog.com!usenet.eel.ufl.edu!news.mathworks.com!uunet!in1.uu.net!spcuna!colcig3.colciencias.gov.co!calvin.univalle.edu.co!jmmFrom: jmm@news.univalle.edu.co (Jorge A. Mejia M.)Newsgroups: comp.unix.programmerSubject: Re: unix c problemz.Date: 2 Aug 1995 18:22:45 GMTOrganization: Universidad del ValleLines: 77Message-ID: <3vofpl$mg5@calvin.univalle.edu.co>References: <3vn4vl$r2l@news1.wolfe.net>NNTP-Posting-Host: cusiana.univalle.edu.coX-Newsreader: TIN [version 1.2 PL2]Cerridwyn Llewyellyn (ceridwyn@wolfe.net) wrote:: hi:  i'm just beginning to learn unix c programming after years of dos.: i'm for some reason having large quantities of trouble doing a seemingly : simple task:  i want a while loop that executes a command continuously : until a key (any key) is presssed:: while (!keypressed) {: 	do_something();: }: simple, eh?  well, in dos, the appropriate function is kbhit(), but i : can't find any such thing in unix... if anyone could send a bit of : example code on how to do what i'm doing, i'd worship you forever. please : mail to ceridwyn@gonzo.wolfe.net.  thanx kindly...Newsgroups: comp.unix.programmerPath: calvin.univalle.edu.co!colcig3.colciencias.gov.co!news.new-york.net!spcuna!solaris.cc.vt.edu!news.alpha.net!news.mathworks.com!europa.chnt.gtegsc.com!howland.reston.ans.net!swrinde!elroy.jpl.nasa.gov!llyene!newsFrom: rreed@bithound.jpl.nasa.gov (Randy Reed)Subject: Re: Unix C programmers, please help!Message-ID: <1995Jul20.165143.22919@llyene.jpl.nasa.gov>Sender: news@llyene.jpl.nasa.govReply-To: rreed@bithound.jpl.nasa.govOrganization: The Unconfigured xvnews peopleReferences: <3uinii$elg@eng_ser1.erg.cuhk.hk>Date: Thu, 20 Jul 1995 16:51:43 GMTLines: 34I have been annoyed since the inception of the C language that an asynchronouskeyboard routine was not part of the standard library.  Anyway, I have been using this version on unix for several years and it seems to do the job. #include <stdio.h>#include <stdlib.h>#include <sys/time.h>#include <sys/ioctl.h>#include <sgtty.h>#define TTY_DESCR 0/*=============================================================== ROUTINE: kbhit PURPOSE: Use select to see if a key has been pressed. RETURNS: 0 If no key has been hit.          1 If a key has been pressed.================================================================*/int kbhit(){    struct timeval wait;    int readfd,ret;    int zero=0;    wait.tv_sec  = 0;    wait.tv_usec = 0;    readfd = 1 << TTY_DESCR;    ret = select(TTY_DESCR + 1, &readfd, &zero, &zero, &wait);    return(ret);} --*********************************                              *	   *      Jorge A. Mejia M.       ** Observatorio Sismologico del **     Suroccidente - OSSO      **    Universidad del Valle     **      Ap. Aereo 25360         **      Cali - Colombia         **                              **   jmm@osso.univalle.edu.co   **                              *	   ********************************-- mitchell@mdd.comm.mot.com (Bill Mitchell)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -