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.

Recommended Answers

All 4 Replies

>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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.