delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2002/05/17/14:31:38

Date: Fri, 17 May 2002 20:28:32 +0200 (MEST)
From: ahelm AT gmx DOT net
To: djgpp AT delorie DOT com
MIME-Version: 1.0
Subject: How to avoid kbhit() and getkey() ?
X-Priority: 3 (Normal)
X-Authenticated-Sender: #0003579562 AT gmx DOT net
X-Authenticated-IP: [194.201.128.4]
Message-ID: <12024.1021660112@www22.gmx.net>
X-Mailer: WWW-Mail 1.5 (Global Message Exchange)
X-Flags: 0001
Reply-To: djgpp AT delorie DOT com

Hi,

I'm using kbhit() and getkey() for simple user interaction
e.g. yes/no or "any key" questions. But this is very DJGPP
specific. Is there a way to write these things more portable,
especially for UNIX environments?

There are 2 main functionalities:
1) empty the keyboard buffer:
while(kbhit())
{
  getkey();
}

2) wait for and read a single key
x = getkey();


A typical application of this might look like:

int yes_no(char *question)
{
  int key   = -1;
  int count = 100;

  while(kbhit())
  {
    getkey();
  }

  if(question)
  {
    printf("%s (Y/N)\n", question);
  }
  else
  {
    printf("(Y/N)\n");
  }

  while(count--)
  {
    key = getkey();

    switch(key)
    {
      case 'y':
      case 'Y':
        return 1;
      case 'n':
      case 'N':
        return 0;
      default:
        break;
    }
  }

  return -1;

} /* End yes_no() */


Any brilliant ideas?

Regards,

Tony

-- 
GMX - Die Kommunikationsplattform im Internet.
http://www.gmx.net

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019