| | |
Listening for keyboard input without pausing the program?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2004
Posts: 42
Reputation:
Solved Threads: 0
How would one listen for keyboard input, without causing the program to pause waiting for the input? For instance say you have something like
And you want it to run indefinitely unless the user enters some key, say "q". And if the user enters "q" it would call some other function, similar to
Also, would there perhaps be a way to in advance tell the program to listen for keyboard input at any point - without explicitly calling a waitforoptionalinput() at a certain point in the program.
C++ Syntax (Toggle Plain Text)
float num = 0; bool runloop = true; for(int i=0; i<1000000 && runloop; i++{ n += some_time_consuming_transformation(i); // How to listen for input at this point? e.g. waitforoptionalinput() function } printf("n is %d\n", n);
C++ Syntax (Toggle Plain Text)
function verify(){ printf("Are you sure you want to stop here? (y/n)\n"); char y_or_n[1]; cin >> y_or_n; if(strcmp("y",y_or_n)==0 || strcmp("Y",y_or_n)==0) runloop = false; }
•
•
Join Date: Mar 2009
Posts: 109
Reputation:
Solved Threads: 12
Its been a while since I used it, but I'm pretty sure it grabs the current key being pressed down and returns its value(was for making a tetris game in class, I can't say I knew all that much back then). As for linux, you can disable the input buffer for cin(or something along the lines of this), which isn't really recommended. Alternatively you can run it on a seperate thread.
![]() |
Similar Threads
- get key input without pausing the program (C++)
- Pygame + Python Keyboard Input Problem (Python)
- How do you get keyboard input while running a standard OpenGL program? (C++)
- Receiving user input to direct program (C++)
- school project: simple C++ game (need help with keyboard input) (C++)
- Keyboard Input (Java)
- Capturing Keyboard Input (C)
Other Threads in the C++ Forum
- Previous Thread: Winsock Multi-Client Servers
- Next Thread: strings and ints
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory microsoft newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets





