![]() |
| | #1 |
| Registered User Join Date: Feb 2010
Posts: 66
| ![]() I was wondering how I could make a program that executes a piece of code for as long as a key is being touched? So for example: Code: while A is pressed down cout<<"You are typing A"<<endl; I can't use Code: std::string key;
while(1){
cin >> key;
if (key == "a"){
cout<<"You are typing A"<<endl;
}
When I type text on my computer, the keyboard will output the letter "a" as I type it. I need something similar to that - which doesn't require Enter/Return. Thanks in advance |
| CPlus is offline | |
| | #2 |
| Registered User Join Date: Nov 2007
Posts: 356
| There is no portable way to do this, so it depends which OS you want to run it on. The easiest ways to do it on Windows and Linux are probably conio.h and curses, respectively.
__________________ I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo |
| NeonBlack is offline | |
| | #3 |
| Super Moderator Join Date: Aug 2001
Posts: 8,415
| The OP never mentioned portable code was one of his requirements. What OS are you using? |
| Bubba is offline | |
| | #4 |
| Registered User Join Date: Feb 2010
Posts: 66
| Hello, thanks for your answers! I am currently using Windows 6, Vista. I checked my Include folder, and did see conio.h How can I use it to do this? Thanks |
| CPlus is offline | |
| | #5 |
| Registered User Join Date: Nov 2007
Posts: 356
| With a loop using kbhit() and getch(). Look up the documentation for these 2 functions. Also I should mention that there is a curses library for Windows called pdcurses, so if you ever might want to port to Linux or Apple, you might want to go that route.
__________________ I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo |
| NeonBlack is offline | |
| | #6 |
| Registered User Join Date: Dec 2009 Location: Henderson, NV
Posts: 887
| Or you can trap the keydown event and continue doing what you are doing until you get a keyup event (and it is your 'A' char or whatever). Different way to skin the cat, so so speak. |
| jeffcobb is offline | |
![]() |
| Tags |
| as long as, automatic, execute, key, no enter |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| My code wont link with gcc but does with Visual Studio | moniker | C Programming | 2 | 12-19-2009 05:08 AM |
| could I got some help to edit this code please | J U S T I N | Linux Programming | 8 | 08-26-2009 10:15 AM |
| Merge and Heap..which is really faster | silicon | C++ Programming | 2 | 05-10-2005 04:06 PM |
| it's a very long code "sorry" | enjoy | C Programming | 11 | 05-03-2004 08:14 AM |
| need help | emperor | C Programming | 1 | 03-04-2002 12:26 PM |