Cin that does not pause...?

Started by
5 comments, last by ToohrVyk 16 years, 11 months ago
I'm making a console app and have hit a bit of a problem. Is it possible to make it so that you can type things in while it is running a loop? I have to keep updating objects but I want the user to be able to type commands (like "quit" or "info") even while the loop is running. It will not work to stop the loop though. Is this possible?
Advertisement
Well, you didn't state what language you're using so I'll assume standard C++.

In a console application, the easiest method of getting input (if the user hits a key) is:

#include <conio.h>...char ch;if ( kbhit() ) ch = getch();


ch will store the key hit.

[Edited by - CaspianB on May 13, 2007 1:44:36 AM]
1) conio.h is not C++, its old C
2) conio.h is not standard, Its borland specific

There is no standard ANSI compatable method of doing this.
If you are using Borland, conio.h could work. If not, you may
need to go through your system API.
Here is one way to do just that in a windows console application.
The vk_get function checks if there is any input in the console input buffer and returns the VK key code if there is, and 0 if there is not.
Im just going to dump the whole thing.
Note that the INPUT_RECORD contains quite a bit of data. The example below only extracts the wVirtualKeyCode
See Win32 console functions for more info.

#include <windows.h>#include <iostream>using namespace std;int vk_get(){    DWORD nevents, mode;    HANDLE ihandle = GetStdHandle(STD_INPUT_HANDLE);	    GetNumberOfConsoleInputEvents(ihandle, &nevents);    if(!nevents) return 0;		    static INPUT_RECORD buffer[1];    GetConsoleMode(ihandle, &mode);    SetConsoleMode(ihandle, 0); // Get rid of line mode    ReadConsoleInput(ihandle, buffer, 1, &nevents);    SetConsoleMode(ihandle, mode);    return nevents ? buffer[0].Event.KeyEvent.wVirtualKeyCode : 0;}int main(){               while(1)    {        int ch = vk_get();                if(ch == VK_ESCAPE)             break;        else if(!ch)             cout << "No press" << endl;        else             cout << "Pressed " << ch << endl;                    Sleep(10);    }}
Quote:Original post by Crypter
1) conio.h is not C++, its old C
2) conio.h is not standard, Its borland specific

There is no standard ANSI compatable method of doing this.
If you are using Borland, conio.h could work. If not, you may
need to go through your system API.


<conio.h> is non-standard. It is not Borland specific. Visual Studio, for example, also provides <conio.h> including getch() and kbhit(), although it prefers you to use _getch() and _kbhit(). Digital Mars C++ provides <conio.h> with getch() and kbhit() as well.

I would guess that <conio.h> implementations for Windows compilers probably just wrap the kind of code pulpfist has posted though.
If you dont care about portability you could use the win32api CreateThread() and stick all your processing code in a seperate thread.
Obligatory reference to PDCurses.

This topic is closed to new replies.

Advertisement