Jump to content
  • Advertisement
Sign in to follow this  
Xloner12

_kbhit() design

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

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.

Share this post


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

Share this post


Link to post
Share on other sites
So your saying instead of using say an entire line grab function like fgets use something that takes each charchter at a time?

Share this post


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

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!