Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I make a "press any key to continue" function?

Status
Not open for further replies.

bdt

Programmer
Sep 29, 2002
9
US
For a cpp program in dos if I wanted to do this I would do a system call to pause like this #include<cstdlib> ... system(&quot;pause&quot;); and that works. unfortunately, &quot;pause&quot; isnt a system command in Unix, although I can press the pause button on my keyboard and produce the desired effects.

Does anyone know the unix equivalent of dos's &quot;pause&quot;, or some other way to accomplish the same thing in Unix?
 
The sleep function should do it. It's argument is the number of seconds you want it to sleep for. man 3 sleep for more info //Daniel
 
danielhozac:

thank you very much for the advice.
i'll give it a shot.

BDT :)
 
I have posted a file here that comes from

you can find a finction here that waits for a character.
the function you need is kb_getc_w(); there are a lot more functions,just four jour information. kb_getc_w is the one completely on the bottom.
Hope this helps.



kbhit.c

/* ***************************************************************************
*
* Copyright 1992-2002 by Pete Wilson All Rights Reserved
* 50 Staples Street : Lowell Massachusetts 01851 : USA
* pete@pwilson.net 978-454-4547
*
* This item is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation; either version 2.1 of the License, or (at your
* option) any later version.
*
* Pete Wilson prepared this item in the hope that it might be useful, but it
* has NO WARRANTY WHATEVER, nor even any implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE.
*
*************************************************************************** */

/* ***************************************************************************
*
* KBHIT.C
*
* Based on the work of W. Richard Stevens, Advanced Programming in
* the Unix Environment,&quot; Addison-Wesley; and of Floyd Davidson.
*
* Contains these functions:
*
* To set the TTY mode:
* tty_set_raw() Unix setup to read a character at a time.
* tty_set_cooked() Unix setup to reverse tty_set_raw()
*
* To read keyboard input:
* kb_getc() keyboard get character, NON-BLOCKING. If a char
* has been typed, return it. Else return 0.
* kb_getc_w() kb get char with wait: BLOCKING. Wait for a char
* to be typed and return it.
*
* How to use:
* tty_set_raw() set the TTY mode to read one char at a time.
* kb_getc() read chars one by one.
* tty_set_cooked() VERY IMPORTANT: restore cooked mode when done.
*
* Revision History:
*
* DATE DESCRIPTION
* ----------- --------------------------------------------
* 12-jan-2002 new
* 20-aug-2002 cleanup
*
*************************************************************************** */

#ifdef __cplusplus
extern &quot;C&quot; {
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <errno.h>

#ifndef STDIN_FILENO
#define STDIN_FILENO 0
#endif

extern int errno;

static struct termios termattr, save_termattr;
static int ttysavefd = -1;
static enum
{
RESET, RAW, CBREAK
} ttystate = RESET;

/* ***************************************************************************
*
* set_tty_raw(), put the user's TTY in one-character-at-a-time mode.
* returns 0 on success, -1 on failure.
*
*************************************************************************** */
int
set_tty_raw(void)
{
int i;

i = tcgetattr (STDIN_FILENO, &termattr);
if (i < 0)
{
printf(&quot;tcgetattr() returned %d for fildes=%d\n&quot;,i,STDIN_FILENO);
perror (&quot;&quot;);
return -1;
}
save_termattr = termattr;

termattr.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
termattr.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
termattr.c_cflag &= ~(CSIZE | PARENB);
termattr.c_cflag |= CS8;
termattr.c_oflag &= ~(OPOST);

termattr.c_cc[VMIN] = 1;
termattr.c_cc[VTIME] = 0;

i = tcsetattr (STDIN_FILENO, TCSANOW, &termattr);
if (i < 0)
{
printf(&quot;tcsetattr() returned %d for fildes=%d\n&quot;,i,STDIN_FILENO);
perror(&quot;&quot;);
return -1;
}

ttystate = RAW;
ttysavefd = STDIN_FILENO;

return 0;
}

/* ***************************************************************************
*
* set_tty_cbreak(), put the user's TTY in cbreak mode.
* returns 0 on success, -1 on failure.
*
*************************************************************************** */
int
set_tty_cbreak()
{
int i;

i = tcgetattr (STDIN_FILENO, &termattr);
if (i < 0)
{
printf(&quot;tcgetattr() returned %d for fildes=%d\n&quot;,i,STDIN_FILENO);
perror (&quot;&quot;);
return -1;
}

save_termattr = termattr;

termattr.c_lflag &= ~(ECHO | ICANON);
termattr.c_cc[VMIN] = 1;
termattr.c_cc[VTIME] = 0;

i = tcsetattr (STDIN_FILENO, TCSANOW, &termattr);
if (i < 0)
{
printf(&quot;tcsetattr() returned %d for fildes=%d\n&quot;,i,STDIN_FILENO);
perror (&quot;&quot;);
return -1;
}
ttystate = CBREAK;
ttysavefd = STDIN_FILENO;

return 0;
}

/* ***************************************************************************
*
* set_tty_cooked(), restore normal TTY mode. Very important to call
* the function before exiting else the TTY won't be too usable.
* returns 0 on success, -1 on failure.
*
*************************************************************************** */
int
set_tty_cooked()
{
int i;
if (ttystate != CBREAK && ttystate != RAW)
{
return 0;
}
i = tcsetattr (STDIN_FILENO, TCSAFLUSH, &save_termattr);
if (i < 0)
{
return -1;
}
ttystate = RESET;
return 0;
}

/* ***************************************************************************
*
* kb_getc(), if there's a typed character waiting to be read,
* return it; else return 0.
*
*************************************************************************** */
unsigned char
kb_getc(void)
{
unsigned char ch;
size_t size;

size = read (STDIN_FILENO, &ch, 1);
if (size == 0)
{
return 0;
}
else
{
return ch;
}
}

/* ***************************************************************************
*
* kb_getc_w(), wait for a character to be typed and return it.
*
*************************************************************************** */
unsigned char
kb_getc_w(void)
{
unsigned char ch;
size_t size;

while (1)
{

usleep(1000); /* 1/10th second*/

size = read (STDIN_FILENO, &ch, 1);
if (size > 0)
{
break;
}
}
return ch;
}
Greetz,
muppeteer.gif

themuppeteer@hotmail.com

Don't eat yellow snow...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top