I have made a function that detects F1 key at the end of the program and starts it again. But what I wanted was that even if user is in the middle of the program when the user presses F1 it immediately restarts the program. Here is my code:

int key(void)
{
int key;
printf("\n");
key = getch();
key = getch();
if (key == 59)
system("cls");
}

int main()
{
my body.....
key(); // at the end of the body.
}

If I place the key() function call at the top of my body... It waits for the user to input F1 so that it can clear screen. I wanted to know how to make it global or an alternative way to make same process even though different code that if the user presses F1 it immediately restarts the program.

>But what I wanted was that even if user is in the middle of the program
>when the user presses F1 it immediately restarts the program.
What you want is an event, or a separate thread that polls for keypresses. Either way you'll be relying on even more nonportable features, so you need to specify your compiler and operating system.

>But what I wanted was that even if user is in the middle of the program
>when the user presses F1 it immediately restarts the program.
What you want is an event, or a separate thread that polls for keypresses. Either way you'll be relying on even more nonportable features, so you need to specify your compiler and operating system.

Compiler: Dev C++
OS: Win XP

Yup, Narue gave you the same answer that I gave you just the other day.

Thx for the tip. Infact I researched it in the net but do you have any example of a code when pressed even in the middle of an example it will repeat or restart a program even in the middle of a running program.

This article has been dead for over six months. Start a new discussion instead.