Unix Programming - Press any key to continue...

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > October 2004 > Press any key to continue...





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Press any key to continue...
John Smith

2004-10-28, 5:51 pm

Hello

What the easiest way to implement "Press any key to continue..." behavior in
the most portable way?

Under windows theres alot of ways like using system() to run the program
pause or using _getch().
What would the easiest way be under Linux?

Thanks in advance.
-- John


Måns Rullgård

2004-10-28, 5:51 pm

"John Smith" <john.smith@x-formation.com> writes:

> Hello
>
> What the easiest way to implement "Press any key to continue..." behavior in
> the most portable way?
>
> Under windows theres alot of ways like using system() to run the program
> pause or using _getch().
> What would the easiest way be under Linux?


getc();

This will require the user to press return, but Unix users expect that
anyway.

--
Måns Rullgård
mru@inprovide.com
Pascal Bourguignon

2004-10-28, 5:51 pm

"John Smith" <john.smith@x-formation.com> writes:
> What the easiest way to implement "Press any key to continue..." behavior in
> the most portable way?


Get some sticker and an indelible pen. Write the word "any" on the
non-sticky face of the sticker. Paste the sticker with the "any" word
up on some key of your keyboard. Provide the user with good spectacles.

--
__Pascal Bourguignon__ http://www.informatimago.com/

Voting Democrat or Republican is like choosing a cabin in the Titanic.
Heiko

2004-10-29, 7:48 am

John Smith wrote:

> What the easiest way to implement "Press any key to continue..." behavior in
> the most portable way?
>
> Under windows theres alot of ways like using system() to run the program
> pause or using _getch().
> What would the easiest way be under Linux?



#include <stdio.h>
#include <termios.h>

void switch_buffering(int onoff)
{
static struct termios stored_settings;
struct termios new_settings;

if (!onoff) {
tcgetattr(0, &stored_settings);
new_settings = stored_settings;
new_settings.c_lflag &= (~ICANON);
new_settings.c_cc[VTIME] = 0;
new_settings.c_cc[VMIN] = 1;
tcsetattr(0, TCSANOW, &new_settings);
} else {
tcsetattr(0, TCSANOW, &stored_settings);
return;
}
}


int main()
{
/*
* Call function to switch off terminal line buffering, so a char
* from stdin will be delivered inmediately.
* Then we can now use getchar() to wait for any key.
*/
switch_buffering(0);

printf("Press any key to continue ");
getchar();

/* Restore original buffering settings. */
switch_buffering(1);

printf("\n");
printf("Continuing...\n");

return 0;
}

/* EOF */


Heiko
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com