Jump to content
  • Advertisement
Sign in to follow this  
Gumgo

Cin that does not pause...?

This topic is 4151 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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);
}
}


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
If you dont care about portability you could use the win32api CreateThread() and stick all your processing code in a seperate thread.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!