input without pressing enter

hi, Im trying to get input without pressing enter

I was wondering if I could achieve this by having one thread do something like this

bool a;
int main(){
int b
a = true;
cin >> b;
sleep(500);}

and have the other thread do this :

void thread2(){
while(1){
if (a==true){
sleep (500) //waiting for cin in other tread to start
cout << (char)(13)
a=false
}
}
}


if not, can you please tell me a way for me to acheive this?

thanks! -blueberry
What are you trying to do? I mean, how will you use this input?
closed account (3hM2Nwbp)
If you are using a Linux terminal, you can adjust the termios structure in order to turn off buffered input. I used this back when I was in school for a terminal based game, and for more user-friendly data entry into the terminal. I'll try to dig up the snippit for you and edit this post if and when I find it.

Best regards,
Luc

*_WARNING_O/S_SPECIFIC_*
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*This code was retyped manually from the terminal*/
/* BufferToggle.hpp
 * @Author
 *    Luc Lieber
 *
 *    A simple class to toggle buffered input
 */

#include <termios.h>
class BufferToggle
{
    private:
        struct termios t;

    public:

        /*
         * Disables buffered input
         */

        void off(void)
        {
            tcgetattr(STDIN_FILENO, &t); //get the current terminal I/O structure
            t.c_lflag &= ~ICANON; //Manipulate the flag bits to do what you want it to do
            tcsetattr(STDIN_FILENO, TSCANOW, &t); //Apply the new settings
        }


        /*
         * Enables buffered input
         */

        void on(void)
        {
            tcgetattr(STDIN_FILENO, &t); //get the current terminal I/O structure
            t.c_lflag |= ICANON; //Manipulate the flag bits to do what you want it to do
            tcsetattr(STDIN_FILENO, TSCANOW, &t); //Apply the new settings
        }
};


_Example_

1
2
3
4
5
6
7
8
9
10
#include <iostream>
int main()
{
    BufferToggle bt;
    std::getchar(); //waits for you to press enter before proceeding to the next instruction
    bt.off();
    std::getchar(); //processes next instruction as soon as you type a character (no enter)
    bt.on();
    std::getchar(); //waits for you to press enter before proceeding to the next instruction
}


Please, feel free to expand upon it in any way you see fit.

http://www.opengroup.org/onlinepubs/007908775/xsh/termios.h.html

Best regards,
Luc
Last edited on
On the otherhand, you can use conio.h (also platform dependent, i believe it's windows only)

1
2
3
4
5
6
7
8
9
10
11
#include <conio.h>
#include <iostream>
using namespace std;

int main()
{
     char key = ' ';
     cout << "Enter a key" << endl;
     key = _getch();
     cout << "You entered: " << key << endl;
}
ya, Im trying to make a terminal based game.

Is there no portable way? A cross platform third party library would work nicely too if there is.

thanks -blueberry
And i don't think getch() and unbuffered input would work cause, Im trying animation using asci art, so, i cant wait for input.
IIRC, the curses libraries ({PD|n}curses) have portable versions of getch() (or whatever the function that gets single keystrokes was).
1
2
3
4
5
6
7
int main()
{
     char key = ' ';
     cout << "Enter a key" << endl;
     key = _getch();
     cout << "You entered: " << key << endl;
}

And i don't think getch() and unbuffered input would work cause, Im trying animation using asci art, so, i cant wait for input.


_getch() will work fine for the purpose of getting input, its just that _getch() is often used in conjunction with _kbhit(). This notifies you if a key has been pressed.

1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
     bool keepGoing = true;
     char key = ' ';
     while (keepGoing){
       cout << "Enter a key" << endl;
       while(_kbhit()){
         key = _getch();
         cout << "You entered: " << key << endl;
       }
     }
}


Of course this is an infinite loop. You must code the following somewhere in your code to end the program under whatever circumstances you deem worthy.

keepGoing = false;
closed account (3hM2Nwbp)
You can use the preprocessor to make the code as portable as you want. In the console based game that I made, there was a user input thread as well as a thread used for the console clearing and redrawing.

1
2
3
4
5
6
7
8
9
10
11

#ifdef _WIN32

    //Windows Implementation

#else

    //Other Implementation

#endif


Best regards,
Luc
Last edited on
oh, I though ncurses was only for UNIX based operating systems. I didn’t know there was a version for the other OSes.

I think i will use a version of curses.

Thanks for your help everybody. I think ill put my code here when Im done.
Oh, thanks. It would of taken me a long time to find them myself.
Topic archived. No new replies allowed.