Jump to content
  • Advertisement
Sign in to follow this  
Grahf750

entering a key without pressing enter

This topic is 4890 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

Is there a way to get a key press without pressing enter in a console app? I tried using conio.h and: if( kbhit() ) { k=getch(); } but every thing flashes that way.

Share this post


Link to post
Share on other sites
Advertisement
In windows, you can use ReadConsole [+kbhit? I don't remember]. I use select/read for unix systems, but that's a bit complex.

Share this post


Link to post
Share on other sites
When you say that teh screen flickers using conio (kbhit). Do you have a loop that prints a menu to screen and whithin the loop you are printing the screen, testing for input (kbhit()). if no input the re-print everything?

You could just use conio...as per your original post and just loop the inour test. Not well explained so I'll provide a little example:

Not too sure what language so I'll use C++


#include <conio>

const char QUIT='Q';

int main
{
char ch = 0;

while (toupper(ch) != QUIT)
{
std::out << "1. Start Game" << std::endl;
std::out << "2. High Score" << std::endl;
std::out << "\nEnter option (press 'q' to quit)" << std::endl;

while (!kbhit()){};

ch = getch();

switch toupper(ch)
{

case '1':
{
startGame();
}
break;

case '2':
{
showHighScores();
}
break;

case 'Q':
{
ch = 'Q';
}
break;

default:
{
std::cout << "Don't know that option - please try again;
}
break;

}

}
}



Hopefully that will stop flickering as the 'screen' isn't being reprinted if there is no input...we just loop until the keyboard is 'hit'.

Not too sure if this goes anywhere near helping you, but hopefully it does.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!