Sign in to follow this  
Xloner12

_kbhit() design

Recommended Posts

Xloner12    265
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
Zahlman    1682
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
Hollower    685
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this