-2

I've seen it once or twice in a few places, but I can't seem to find this pattern while searching. What I want is, while in a loop, pause for a couple of milliseconds for user input, then continue.

so it would look something like this:

int main()
{

    while (1)
    {
        //do stuff
        //get user input, continue if there is nothing
        //do stuff based off of user input
    }
}

I'm using xemacs and g++ on Fedora 18 and I'm looking for a single keyboard keypress.

McAden
  • 13,714
  • 5
  • 37
  • 63
DForck42
  • 19,789
  • 13
  • 59
  • 84
  • This isn't entirely portable, so we pretty much need to know what platform(s) you care about to answer. – Jerry Coffin Feb 11 '14 at 20:27
  • 1
    What kind of input are you looking for? Mouse, keyboard, joystick? If it's the keyboard, are you looking for a single key press or a combination of keystrokes? Your question needs to be a bit more specific. – vane Feb 11 '14 at 20:27
  • added additional info – DForck42 Feb 11 '14 at 20:37
  • possible duplicate of [C/C++: Capture characters from standard input without waiting for enter to be pressed](http://stackoverflow.com/questions/421860/c-c-capture-characters-from-standard-input-without-waiting-for-enter-to-be-pr) – Mooing Duck Feb 11 '14 at 21:04

2 Answers2

1

You'll (almost certainly) want to use the curses library.

If you want to pause to wait for input, you call halfdelay to set the time to wait for input. If the user hasn't entered anything in that time, it'll return ERR (or some error code anyway--been a while so I can't remember for sure what). If memory serves, however, the shortest delay you can specify is 1/10th of a second; there's no way to tell it to just pause for (say) up to 2 ms. Edit: thinking about it, it seems like there is a timeout (or something like that) that lets you set a timeout in milliseconds.

More often, you just want to react to input if there is any, and just continue other processing if here is none. In this case, you'd call nodelay.

Either way, you also want to call cbreak to disable line buffering at the OS level, so any key the user presses will be available to the program immediately instead of waiting for them to press return before you receive the input.

I'd guess you probably also want to call noecho() so the user's input isn't echoed by getch. For the kind of program you seem to be talking about, echoing the input command is rarely desired.

Then inside your loop, you'll call getch() to get the key input data (if any). If you get input, you process it. If it returns the error code to indicate there's no input, you continue your other processing.

So the code would be structured something like:

cbreak();
noecho();
nodelay(mywindow, true);

while (1) {
    int ch;
    if (ERR != (ch = getch()))
        process(ch);
    update_screen();
    do_other_processing();
}
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
0

In the case of wanting a keyboard input, and then firing a trigger only if it is there, your best bet is essentially to use an event handler style system, something akin to how games detect input. What happens is usually something like this:

Event e;
while(Poll for event e) // Basically check if something has been triggered.
{
    if(event type == certain key pressed){
        // Do related stuff
    }
}

You will essentially put this inside your main program loop, and every time that key, or some other trigger you specify is fired, it will call a function or do something else you specified.

Rivasa
  • 6,510
  • 3
  • 35
  • 64