_kbhit() design

Started by
3 comments, last by Xloner12 16 years ago
Im using while(!_kbhit()) to check and not break till some one touches the keyboard. Now as soon as some one touches a key, I take that keystroke and combine it with what there currently typing and thats the sentence they typed so basicly the _kbhit key+fget string = the whole sentance. My problem is if they type (H ow is every one doing) I printf the H so that they can see it like the rest of the echo'ed text but they can't go back and delete it like the other text because its printed. Is there any way around this problem other then having them hit a key first then type the word. I want them to be able to just start typing there sentance. Im bad at explaining stuff so please any questions and I'll try to clarify.
Do or do not there is no try -yoda
Advertisement
Don't use line mode. Do whatever is necessary on your platform to put the system into character mode, and implement the deletion "manually". I.e. receive a character at a time: if it's not a backspace, just add it to your program's internal buffer and display it, but if it is, echo backspace, space, backspace, and remove a character from the buffer instead of adding one.
So your saying instead of using say an entire line grab function like fgets use something that takes each charchter at a time?
Do or do not there is no try -yoda
Here is a really simple example from an old app of mine, with comments.

void Console::getInput(){	if ( _kbhit() )					// if a keypress is detected	{		char c = static_cast<char>(_getch());	// get the character				if (c == '\r')				// enter key		{			if (buffer.empty())		// nothing was typed			{				prompt();				return;			}			if (buffer == "quit" || buffer == "exit")	// exit command was typed			{				active = false;				cout << endl;				return;			}						cout << endl;			queueCommand(buffer);			buffer.clear();		}		else if (c == '\b')			// backspace key		{			if (buffer.empty())		// nothing to erase				return;			buffer.erase(buffer.end()-1, buffer.end());	// erase last char from buffer			cout << "\b \b";				// erase last char from screen		}		else					// any other char		{			buffer += tolower(c);		// append lowercase char to buffer			cout << c;			// print it		}	}}


Further explanation:
buffer is a std::string member of the Console class.
active is a bool member of Console.
prompt is a method which simply displays a command prompt.
queueCommand is a method which copies the string to a queue to await command parsing.

Elsewhere there is a loop which basically looks like this:
while (console.isActive()){	console.getInput();	console.executePendingCommand();	// ... other non-blocking tasks ...	Sleep(0);}


executePendingCommand pops a string from the front of the queue, does some parsing, etc.
You probably don't need the queueing stuff. I had it to support multiple commands like macros or scripts.
Ok, im working with C though so I'll go ahead and adapt that then. Thanks.
Do or do not there is no try -yoda

This topic is closed to new replies.

Advertisement