Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What code should I use to execute this logic:

  • Continue to loop until the user presses a key pressed, at which point the program will pause.
  • If the user presses a key again, then resume the loop.
  • If the user presses a key again, then stop the loop completely (i.e., quit the program).
Posted
Updated 16-Nov-11 11:04am
v2
Comments
AspDotNetDev 16-Nov-11 17:07pm    
I improved your question. However, it is not clear if you are talking about different keys for each event (pause, resume, quit) or if each event uses a different key (e.g., ENTER to pause, SPACE to resume, ESC to quit). Also, it is not clear if you would like each event to only happen once in the other you specified, or if they can happen anytime and any number of times (e.g., pause, resume, pause, resume, pause, resume, quit). Please edit your question to clarify what you are looking for. Also, let us know exactly what you are having trouble with specifically (intercepting key presses, what to do when the loop is paused, quit the program, and so on).
Albert Holguin 16-Nov-11 17:07pm    
Is this just in plain C? No framework?
Mohibur Rashid 16-Nov-11 18:00pm    
what platform are you using? is it window based or console based application?
TRK3 16-Nov-11 18:21pm    
There is nothing in the C standard for getting the state of the keyboard.

The C standard only specifies stdin as an input source. In most OS implementations, key presses are translated to characters and become available on stdin only after the user hits enter.

If it's acceptable to have the user hit enter to make something happen, then you can poll stdin without blocking by seeking to the end and seeing if the position changed:

pos = ftell(stdin);
fseek (stdin, 0, SEEK_END);
newpos = ftell (stdin);

If that's not acceptable, then your solution is going to be operating system / machine / framework dependent.

Tell us what environment you are working in and we can help you figure out how to do it.

run the process in a different thread. main thread will read the key stroke and increase the value from t from 0 to higher.

there is another function in stdlib that detect keystroke i cannot recall that function dont wait for key stroke...
C++
void runningProcess()
{ 
 while(1)
 {
    if(t==1)   //t is a global variable;
    {
     while(1)
     {
      if(t==2)
      {
       break;
      } 
     }
    }
    else if(t==3)
    {
      return;
    }
 }
}
 
Share this answer
 
Comments
TRK3 16-Nov-11 19:31pm    
I actually like your solution -- it's what I thought to recommend at first, but you still can't do it in standard C.

The only thing in the standard C runtime is getchar() -- which won't return until you hit enter in most environments.

Also, there is nothing in the C standard for creating separate threads -- it's going to be specific for the environment he is programming for.


It's probably simpler to figure out the non-blocking keyboard polling mechanism for the environment he is programming in than it is to figure out the threading library for that environment and set up the thread.
C#
void module()
{
    int notexit = 2;
    while(notexit)
    {
        if(kbhit())
        {
            if(notexit != 1)
                getch();
            notexit--;
        }
    }
}
 
Share this answer
 
v2
Comments
André Kraak 17-Nov-11 7:00am    
Edited question:
Formatted code

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900